regex - What is the difference between regexp.Compile and regexp.CompilePOSIX? -
can provide examples explain differences between regexp.compile
, regexp.compileposix
? read documentation. can't intuitive understanding.
perl- , posix-compatible regular expressions similar in large parts, differ in key aspects, example submatching. mentioned here:
posix defines resolve submatches, first chose match starts leftmost in string. (this traditional perl behavior here things diverge.) among submatches starting @ leftmost position in string, choose longest 1 overall.
say have regular expression (foo|foobar)
. when matching expression against string match several of subexpressions (for example, foobarbaz
match both subpatterns, foo
, foobar
), perl-compatible regex return first match (foo
), while posix-compatible regex return longest match (foobar
).
some example code (playground):
package main import "fmt" import "regexp" func main() { pattern := "(foo|foobar)" str := []byte("foobarbaz") rpcre, _ := regexp.compile(pattern) rposix, _ := regexp.compileposix(pattern) matchespcre := rpcre.find(str) fmt.println(string(matchespcre)) // prints "foo" matchesposix := rposix.find(str) fmt.println(string(matchesposix)) // prints "foobar" }
Comments
Post a Comment