Does the Python regex module use BRE or ERE?

POSIX seems to separate regular expression implementations into two types: Basic Regular Expressions (BRE) and Extended Regular Expressions (ERE).

Python redoes not indicate.

+4
source share
2 answers

Except for some similarities in syntax, the module re does not comply with the POSIX standard for regular expressions .

Different semantics of correspondence

POSIX ( DFA/NFA ) , re - , " " ( " " , ).

(Prefix|PrefixSuffix) PrefixSuffix.

  • POSIX- POSIX- ( , ), PrefixSuffix.
  • , re ( - ) Prefix, Prefix .

(xxx|xxxxx)* xxxxxxxxxx ( 10 x):

  • Cygwin:

    $ [[ "xxxxxxxxxx" =~ (xxx|xxxxx)* ]] && echo "${BASH_REMATCH[0]}"
    xxxxxxxxxx
    

    10 x .

  • Python:

    >>> re.search(r'(?:xxx|xxxxx)*', 'xxxxxxxxxxx').group(0)
    'xxxxxxxxx'
    

    9 x, xxx 3 , )

POSIX-

, POSIX , . strong > . .

:

, , . [...]. ("[=" "=]") . , 'a', 'ร ' 'รข' , "[[=a=]b]", "[[=ร =]b]" "[[=รข=]b]" "[aร รขb]", [...]

, - . .

re

re Perl, Perl re. , re, POSIX:

  • / , .

    * POSIX , POSIX. "" .

  • ( )
  • (?(id/name)yes-pattern|no-pattern)
  • : \b, \s, \d, \w ( POSIX , undefined )
+5

. PCRE, .

re :

, , Perl.

, , , POSIX, , Perl 4 Perl 5 , , POSIX grep -E aka ERE.

perlre , Python. Perl :

, Perl, , 8. ( () , V8.)

( V8 8 Unix. Spencer (re) POSIX.)

Perl 4 , \d, \s, \w, , \t, \f, \n. Perl 5 ( ), , ,

  • Unicode
  • Lookaheads and lookbehinds
  • ... , (?

"" "".

, Exim; PCRE , (Ruby, PHP, Python ..). , , " Perl" (); , . (, Perl * {0,32767}, PCRE - .)

Python , ( PCRE), Python 2.7/3.5.

+4

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


All Articles