I'd like to use a regular expression which matches the beginning of a line in a text. For some reason, ^
does not work, see this failing test:
func TestNewLine(t *testing.T) {
re := regexp.MustCompile("^bar")
match := re.FindString("foo
bar
baz")
assert.Equal(t, "bar", match)
}
What do I miss?
You have to enable multi line mode flag for regex evaluation. Try this:
func TestNewLine(t *testing.T) {
re := regexp.MustCompile("(?m)^(bar)")
match := re.FindString("foo
bar
baz")
assert.Equal(t, "bar", match)
}