Regular expression to extract date(YYYY-MM-DD) from string
Example
package main
import (
"fmt"
"regexp"
)
func main() {
str1 := "If I am 20 years 10 months and 14 days old as of August 17,2016 then my DOB would be 1995-10-03"
re := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`)
fmt.Printf("Pattern: %v\n", re.String()) // print pattern
fmt.Println(re.MatchString(str1)) // true
submatchall := re.FindAllString(str1, -1)
for _, element := range submatchall {
fmt.Println(element)
}
}
Output
Pattern: \d{4}-\d{2}-\d{2}
true
1995-10-03
Most Helpful This Week
Sierpinski Carpet in Go Programming Language
Subtract N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time.
How to check lowercase characters in a string in Golang?
How to create thumbnail of an image?
Example Function that takes an interface type as value and pointer?
How to read current directory using Readdir?
Golang HTML parser
Replace first occurrence of string using Regexp
Replace any non-alphanumeric character sequences with a dash using Regex
How to read input from console line?