dlclark / regexp2

A full-featured regex engine in pure Go based on the .NET engine
MIT License
997 stars 84 forks source link

Valid regex doesn't compile #20

Closed lozaericml closed 5 years ago

lozaericml commented 5 years ago

Hi!

If pattern contains \_, regexp2 fails to compile it. Example:

_, err := regexp2.Compile("^/legacy/([\w|\d|\-\_]+)/([\w|\d|\-\_]+)/.*", 0)
if err != nil {
    fmt.Println(err)
}

Error is error parsing regexp: unrecognized escape sequence \_ in ^/legacy/([\w|\d|\-\_]+)/([\w|\d|\-\_]+)/.*)

This pattern works in regexp package.

thanks!

dlclark commented 5 years ago

Hello! The regexp2 engine matches the behavior of the C# regular expression engine and not all patterns that are valid for the Go stdlib regexp package are valid for regexp2 (and vise versa). This pattern gives the same error in the C# engine, so this is as-designed.

A couple notes on your pattern:

I think the pattern you want is: ^/legacy/([\w\d\-]+)/([\w\d\-]+)/.*

Thanks, Doug