I can find't an official reference for the precedence rules for Perl regular expressions. What I can find is only Know the precedence of regular expression operators. However, it's not an official reference given by perldoc.
In regular expressions, the ' * ', ' + ', and ' ? ' operators, as well as the braces ' { ' and ' } ', have the highest precedence, followed by concatenation, and finally by ' | '. As in arithmetic, parentheses can change how operators are grouped.
The regular-expression operator star has the highest precedence and is left associative. The regular-expression operator concatenation has the next highest precedence and is left associative.
Regular Expression (Regex or Regexp or RE) in Perl is a special text string for describing a search pattern within a given text. Regex in Perl is linked to the host language and is not the same as in PHP, Python, etc. Sometimes it is termed as “Perl 5 Compatible Regular Expressions“.
Regular expressions only have two binary operators, one of which is implicit rather than represented by a symbol. Regular expressions also have a number of unary operators, but their precedence is moot due to the restrictions on their operands. That makes talking about precedence really odd.
It's simpler conveying the information you seek using the following statements:
The above information is conveyed one way or another in perlretut.
That said, it is possible to build a precedence table. Since the above statements convey all the information you need, it's possible to build the precedence table from them. It is the following:
a
, \n
, \^
, .
, ^
, \w
, [...]
, \1
, (...)
)This matches the chart in the page to which you linked.
For fun, the following would be the BNF:
pattern ::= <alternation>
alternation ::= <sequence> <alternation2>
alternation2 ::= "|" <alternation> | ""
sequence ::= <quantified_atom> <sequence> | ""
quantified_atom ::= <atom> <quantified_atom2>
quantified_atom2 ::= <modified_quantifier> | ""
modified_quantifier ::= <quantifier> <modified_quantifier2>
modified_quantifier2 ::= <quantifier_modifier> | ""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With