Home  >  Article  >  Backend Development  >  Why Doesn\'t Go Regex\'s Dot Character Match Newlines By Default?

Why Doesn\'t Go Regex\'s Dot Character Match Newlines By Default?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-27 21:23:02875browse

Why Doesn't Go Regex's Dot Character Match Newlines By Default?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn