Regular Expressions



Must start with what is specified after ^
Example: ^xy   Matches: xy, xyz


Must end with what is specified before $
Example: xy$ Matches: xy, mxy


Any character except \n
Example: x.y Matches: xoy, xly
One of the two alternates
Example: one | two Matches: one, two


Quantifies the occurence
Example: xy{3}z Mathes:xyxyz


Groups part of the expression
Example: (xy){3} Mathes: xyxyxy


Explicitly specify characters to match
Example: x[om]y Matches: xoy, xmy


Zero or more of previous expression
Example: xy*z Matches: xz, xyz, xyyz, xyyyz


One or more of previous expression
Example: xy+z Matches:  xyz, xyyz, xyyyz


Zero or one of previous expression. Highest precedence matching when an expression matches serveral strings
Example: xy?z Matches:  xz, xyz


It makes a literal of metacharacters
Also used for special character matching
Example: x\*z Matches:  x*z
a\sc Matches: a c

Character Escapes


. $ ^ { [ ( | ) ] } * + ? \
All other characters match, as provided


\a bell (alaram) \u0007
\b backspace \u0008 when in bracket otherwise specifies word boundary between \w and \W
\t tab \u0009
\r carriage return \u000D
\v vertical tab \u000B
\f form feed \u000C
\n new line \u000A
\e escape \u001B
\O40 ASCII character as octal (up to 3-digits). \O40 represents space
\x20 ASCII character as hex (up to 2-digits)
\cC ASCII control character. Ctrl-C
\u0020 Unicode character using hex (must be 4-digits)
\? matches ?, a metacharacter. simillarly \* matches *

Classes of Characters - Shortcuts

[aeiou] match with any of the specified characters
[^aeiou] should NOT match with any of the specified characters. ^ has a different meaning than start with (see Metacharacters above)
[0-9a-fA-F] must be 0 through 9, a through f or A through F
\p{name} match with any character in class specified by name. Examples of character classes: Ll, Nd, Z, IsGreek, IsBoxDrawing
\w match any word character. Equivalent to [a-zA-Z_0-9]
\W NON-word character. Equivalent to [^a-zA-Z_0-9]
\s match any white-space character.
\S match any NON-white-space character. Equivalent to [^\f\n\r\t\v]
\d match any decimal digit. Equivalent to [0-9]
\D match any non-digit. Equivalent to [^0-9]


^[a-zA-Z0-9',.\s]{1,40}$  - A string composed of 1 to 40 characters that are alphanumeric, apostrophe, comma, period or spaces. Must start with specified characters (^) and must end with specified characters ($). Must be between 1 and 40 characters as specified by the quantifier {1, 40}.

 [^a-zA-Z0-9]- All characters except the ones specified here.

^(?=.*[a-zA-Z])(?=.*\d)\S{8,25}$ - Must contain at least one alphabet and one number. Must be at least 8 characters long and at most 25 characters long.

^[1-9]\d*(\.\d+)?$ - Decimal numbers greater than zero

0|^[1-9]\d*(\.\d+)?$ - Zero any positive decimal number

^[1-9]\d*$ - Positive integer

0$|^[1-9]\d*$ - 0 or positive integer

^[1-9]\d{0,9}$ - Positive integer up to 10 digits

0$|^[1-9]\d{0,9}$ - 0 or positive integer up to 10 digits