Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Regular Expression – Operator Precedence

Regular Expression - Operator Precedence

Regular expressions are evaluated from left to right following a precedence order, much like arithmetic expressions.

Operations with the same precedence are evaluated left to right, while operations with different precedence are evaluated high to low. The table below explains the precedence order of 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 Anchors and sequences (i.e., position and order)
| Alternation, the "or" operation
The character has higher precedence than the alternation operator, so "m|food" matches "m" or "food". To match "mood" or "food", use parentheses to create a subexpression, resulting in "(m|f)ood".

The following are some common regular expression operators listed from highest to lowest precedence:

Next, let us look at the following explanations of regular expression precedence:

\d{2,3}|[a-z]+(abc)*