What is the difference between regexp.Compile and regexp.CompilePOSIX?

Can someone give some examples to explain the differences between regexp.Compileand regexp.CompilePOSIX? I read the documentation. But I can not understand the intuitive understanding.

+3
source share
1 answer

Perl and POSIX compatible regular expressions are mostly similar, but differ in some key aspects, such as fake . This is mentioned here :

POSIX , , . ( Perl, .) , , .

, (foo|foobar). , (, foobarbaz , foo foobar), Perl- (foo), PREIX- (foobar).

( ):

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"
}
+5
source

Source: https://habr.com/ru/post/1624589/


All Articles