Go Regex: Dot Character and Newline Matching
The Go re2 syntax documentation states that the dot character (.) matches any character, including newlines when the "single line" mode (s) is enabled. However, a simple program reveals that the dot character does not match newlines by default.
Program:
<code class="go">package main import ( "fmt" "regexp" ) func main() { text := "foo\nbar\nbaz" pattern := `foo.*bar` matched, err := regexp.MatchString(pattern, text) if err != nil { fmt.Println(err) return } fmt.Println(matched) }</code>
Result:
false
The output shows that the pattern does not match the text, even though the text contains "foo" and "bar" separated by a newline.
Reason
Like many other regex engines, the dot character in Go does not match newlines by default. To enable newline matching, the "dot all" flag (?s) must be added to the regex:
<code class="go">pattern := `foo.*?sbar`</code>
With the "dot all" flag enabled, the regex will match the text as expected:
true
Therefore, to match any character, including newlines, in Go regexp, the "dot all" flag (?s) must be used.
The above is the detailed content of Why Doesn\'t Go Regex\'s Dot Character Match Newlines By Default?. For more information, please follow other related articles on the PHP Chinese website!