Loading....
Regular Expression | Will match... |
foo | The string "foo" |
^foo | "foo" at the start of a string |
foo$ | "foo" at the end of a string |
^foo$ | "foo" when it is alone on a string |
[abc] | a, b, or c |
[a-z] | Any lowercase letter |
[^A-Z] | Any character that is not a uppercase letter |
(gif|jpg) | Matches either "gif" or "jpeg" |
[a-z]+ | One or more lowercase letters |
[0-9\.\-] | ?ny number, dot, or minus sign |
^[a-zA-Z0-9_]{1,}$ | Any word of at least one letter, number or _ |
([wx])([yz]) | wy, wz, xy, or xz |
[^A-Za-z0-9] | Any symbol (not a number or a letter) |
([A-Z]{3}|[0-9]{4}) | Matches three letters or four numbers |
Regular Expressions Reference Sheet | ||
Character | Definition | Example |
^ | The pattern has to appear at the beginning of a string. | ^cat matches any string that begins with cat |
$ | The pattern has to appear at the end of a string. | cat$ matches any string that ends with cat |
. | Matches any character. | cat. matches catT and cat2 but notcatty |
[] | Bracket expression. Matches one of any characters enclosed. | gr[ae]y matches gray or grey |
[^] | Negates a bracket expression. Matches one of any characters EXCEPT those enclosed. | 1[^02] matches 13 but not 10 or12 |
[-] | Range. Matches any characters within the range. | [1-9] matches any single digit EXCEPT 0 |
? | Preceeding item must match one or zero times. | colou?r matches color or colour but not colouur |
+ | Preceeding item must match one or more times. | be+ matches be or bee but not b |
* | Preceeding item must match zero or more times. | be* matches b or be orbeeeeeeeeee |
() | Parentheses. Creates a substring or item that metacharacters can be applied to | a(bee)?t matches at or abeet but not abet |
{n} | Bound. Specifies exact number of times for the preceeding item to match. | [0-9]{3} matches any three digits |
{n,} | Bound. Specifies minimum number of times for the preceeding item to match. | [0-9]{3,} matches any three or more digits |
{n,m} | Bound. Specifies minimum and maximum number of times for the preceeding item to match. | [0-9]{3,5} matches any three, four, or five digits |
| | Alternation. One of the alternatives has to match. | July (first|1st|1) will match July 1stbut not July 2 |
POSIX Character Classes | ||
Character | Definition | Example |
[:alnum:] | alphanumeric character | [[:alnum:]]{3} matches any three letters or numbers, like 7Ds |
[:alpha:] | alphabetic character, any case | [[:alpha:]]{5} matches five alphabetic characters, any case, like aBcDe |
[:blank:] | space and tab | [[:blank:]]{3,5} matches any three, four, or five spaces and tabs |
[:digit:] | digits | [[:digit:]]{3,5} matches any three, four, or five digits, like 3, 05, 489 |
[:lower:] | lowercase alphabetics | [[:lower:]] matches a but not A |
[:punct:] | punctuation characters | [[:punct:]] matches ! or . or , but not a or 3 |
[:space:] | all whitespace characters, including newline and carriage return | [[:space:]] matches any space, tab, newline, or carriage return |
[:upper:] | uppercase alphabetics | [[:upper:]] matches A but not a |
Perl-Style Metacharacters | ||
Character | Definition | Example |
// | Default delimiters for pattern | /colou?r/ matches color or colour |
i | Append to pattern to specify a case insensitive match | /colou?r/i matches COLOR orColour |
\b | A word boundary, the spot between word (\w)and non-word (\W) characters | /\bfred\b/i matches Fred but notAlfred or Frederick |
\B | A non-word boundary | /fred\B/i matches Frederick but notFred |
\d | A single digit character | /a\db/i matches a2b but not acb |
\D | A single non-digit character | /a\Db/i matches aCb but not a2b |
\n | The newline character. (ASCII 10) | /\n/ matches a newline |
\r | The carriage return character. (ASCII 13) | /\r/ matches a carriage return |
\s | A single whitespace character | /a\sb/ matches a b but not ab |
\S | A single non-whitespace character | /a\Sb/ matches a2b but not a b |
\t | The tab character. (ASCII 9) | /\t/ matches a tab. |
\w | A single word character - alphanumeric and underscore | /\w/ matches 1 or _ but not ? |
\W | A single non-word character | /a\Wb/i matches a!b but not a2b |