What are the precedence rules for Perl regular expressions?

I cannot find the official link for the precedence rules for Perl regular expressions . I can only find Know the priority of regular expression operators . However, this is not an official link given by perldoc .

+5
source share
1 answer

Regular expressions contain only two binary operators, one of which is implicit, and not indicated by a symbol. Regular expressions also have a number of unary operators, but their priority is controversial due to restrictions on their operands. This suggests that the priority is really strange.

This simplifies the transfer of the information you are looking for using the following statements:

  • Quantifiers modify one atom.
  • Quantifier modifiers modify one quantifier.
  • Alternation without restrictions, with the exception of the partners in which they live.

The above information is transmitted one way or another in perlretut .


However, you can build a table of priorities. Since the above statements convey all the information you need, you can build a priority table from them. This is the following:

  • Atoms (for example, a , \n , \^ , . , ^ , \w , [...] , \1 , (...) )
  • Unary Postfix operators (quantifiers and quantifier modifiers)
  • The implicit “followed by” operator between (possibly quantized) atoms
  • Alternation

This corresponds to the diagram on the page that you linked to.


For entertainment, 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> | "" 
+4
source

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


All Articles