Skip to content

Commit 5596495

Browse files
committed
fixes #44 change RE2 option to allow default character escape sequences
1 parent d0559a0 commit 5596495

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ The default behavior of `regexp2` is to match the .NET regexp engine, however th
8181
* add support for python-style capture groups (e.g. `(P<name>re)`)
8282
* change singleline behavior for `$` to only match end of string (like RE2) (see [#24](https://github.com/dlclark/regexp2/issues/24))
8383
* change the character classes `\d` `\s` and `\w` to match the same characters as RE2. NOTE: if you also use the `ECMAScript` option then this will change the `\s` character class to match ECMAScript instead of RE2. ECMAScript allows more whitespace characters in `\s` than RE2 (but still fewer than the the default behavior).
84+
* allow character escape sequences to have defaults. For example, by default `\_` isn't a known character escape and will fail to compile, but in RE2 mode it will match the literal character `_`
8485

8586
```go
8687
re := regexp2.MustCompile(`Your RE2-compatible pattern`, regexp2.RE2)

regexp_re2_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,30 @@ func TestRegularSpace(t *testing.T) {
194194
t.Fatal("Expected no match")
195195
}
196196
}
197+
198+
func TestEscapeLiteralDefaults(t *testing.T) {
199+
_, err := Compile(`a\_test`, 0)
200+
if err == nil {
201+
t.Fatal("Expected compile fail")
202+
}
203+
204+
r := MustCompile(`a\_test`, RE2)
205+
if m, _ := r.MatchString("a_test"); !m {
206+
t.Fatal("Expected match")
207+
}
208+
if m, _ := r.MatchString("a\\_test"); m {
209+
t.Fatal("Expected no match")
210+
}
211+
}
212+
213+
/*
214+
func TestRE2EndZ_Singleline(t *testing.T) {
215+
// PCRE allows for \n after the $ and RE2 doesn't
216+
r := MustCompile(`^ac$\Z`, RE2|Debug)
217+
if m, _ := r.MatchString("ac"); m {
218+
t.Fatal("Expected no match")
219+
}
220+
if m, _ := r.MatchString("ac\n"); m {
221+
t.Fatal("Expected no match")
222+
}
223+
}*/

syntax/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1715,7 +1715,7 @@ func (p *parser) scanCharEscape() (r rune, err error) {
17151715
case 'c':
17161716
r, err = p.scanControl()
17171717
default:
1718-
if !p.useOptionE() && IsWordChar(ch) {
1718+
if !p.useOptionE() && !p.useRE2() && IsWordChar(ch) {
17191719
return 0, p.getErr(ErrUnrecognizedEscape, string(ch))
17201720
}
17211721
return ch, nil

0 commit comments

Comments
 (0)