Regular expressions overview
Definition and fundamental properties
Regular expressions are a form of language which provide concise and flexible means to match strings of text such as characters, words, or patterns of characters.
They are useful for matching product codes, phone numbers, license plates and everything featuring a fixed pattern with variable content.
The followings are examples of how regular expressions are used.
Consider this regular expression:
[\dA-Z\-\#]+
It matches any sequence of one or more (+
) digits (\d
) or uppercase letters (A-Z
) along with some non-alphanumeric characters (-
and #
). This expression will then match, for example, US license plates such as 63R2189, DGM529 or 14Y2692.
The following expression:
\"[^\"]+\"
matches any sentence or word typed between quotes. For example, in a sentence like this:
"The market is not operating in a normal way," Mr. Bernanke said on that August call, in a moment of historic understatement.
the regular expression would match:
"The market is not operating in a normal way,"
Regular expressions reference
There are several types of regex engines on the market, each supporting its own regular expressions dialect. The PATTERN
attribute supports Perl compatible regular expressions.
Tip
You can use the regex101 web app to test your regular expressions. Select the PCRE2 (PHP) flavor and enable the Anchored option to faithfully reproduce the behavior of the regular expression syntax of the PATTERN
attribute.