Regular expressions are evaluated from left to right and follow the order of priority, which is very similar to arithmetic expressions.

The operation with the same priority is performed from left to right, and the operation with different priority is high and then low. The following table illustrates the precedence order of the various regular expression operators from highest to lowest:

Operator

Description

\

Escape character

(), (?:), (?=), []

Parentheses and square brackets

*, +, ?, {n}, {n,}, {n,m}

Qualifier

^, $, \ Any metacharacter, any character

Locate points and sequences (i.e., position and order)

|

Replace, OR operation

Characters take precedence over the replacement operator so that “m | food” matches “m” or “food”. To match “mood” or “food”, use parentheses to create a subexpression that produces “(m | f) ood”.