Regular expressions - operator precedence


Regular expressions are evaluated from left to right and follow the order of precedence, much like arithmetic expressions.

Operations with the same priority are performed from left to right, and operations with different priorities are performed first and then low. The following table illustrates the order of precedence of the various regular expression operators from highest to lowest:

OperatorDescription
\Escape characters
(), (?:), (?=), []circle Parentheses and square brackets
*, +, ?, {n}, {n,}, {n,m}Qualifier
^, $, \any metacharacter, any characterAnchor points and sequences (ie: position and order)
|                   Replacement, "or" operation
Characters have higher precedence than the replacement operator such that "m|food" matches "m" or "food". To match "mood" or "food", use parentheses to create a subexpression, resulting in "(m|f)ood".