Examples
This section provides some practical examples of regular expressions. Double quotes are used as the delimiter for these examples.
The following regular expression matches any word of normal text:
"[A-Za-z][a-z-]* "
The regular expression contains a space at the end, after the *, to indicate the end of the word.
Two regular expressions that match phone numbers are:
"^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$"
Matches phone numbers of the format (nnn) nnn-nnnn. Notice the parentheses are escaped. A more flexible regular expression might be:
"^[0-9)( -]{7,20}$"
Matches a string that can contain numbers, parentheses, spaces, and dots. The string must be at least 7 characters long but not more than 20 characters long.
To match a zip code, including ...
Get Spring Into Linux® now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.