Regular expression quick start guide
From TBwiki
(Difference between revisions)
Line 29: | Line 29: | ||
| ''/[0-9]/'' matches a single digit<br> | | ''/[0-9]/'' matches a single digit<br> | ||
|- | |- | ||
− | | [\d]<br> | + | | [\d]<br> |
− | | Shorthand character classes matching digits. Same as [0-9].<br> | + | | Shorthand character classes matching digits. Same as [0-9].<br> |
| ''/[\d]/'' matches a single digit<br> | | ''/[\d]/'' matches a single digit<br> | ||
|- | |- | ||
− | | .<br> | + | | .<br> |
− | | Dot matches any characters.<br> | + | | Dot matches any characters.<br> |
| ''/a.c/'' matches both "a4c" and "ayc"'''<br>''' | | ''/a.c/'' matches both "a4c" and "ayc"'''<br>''' | ||
|- | |- | ||
| ^ | | ^ | ||
− | | Matches at the start of the string the regex pattern is applied to. Matches a position rather than a character. | + | | Matches at the start of the string the regex pattern is applied to. Matches a position rather than a character. |
| <br> | | <br> | ||
|- | |- | ||
| $ | | $ | ||
− | | Matches at the end of the string the regex pattern is applied to. Matches a position rather than a character. | + | | Matches at the end of the string the regex pattern is applied to. Matches a position rather than a character. |
| <br> | | <br> | ||
|- | |- | ||
− | | {m,n}<br> | + | | {m,n}<br> |
− | | Matches at least “m” and at most “n” occurrences of preceeding character, character class or group.<br> | + | | Matches at least “m” and at most “n” occurrences of preceeding character, character class or group.<br> |
| <br> | | <br> | ||
|- | |- | ||
Line 54: | Line 54: | ||
|- | |- | ||
| + | | + | ||
− | | Matches '''one '''or '''more '''occurrences of preceeding character, character class or group.'''<br>''' | + | | Matches '''one '''or '''more '''occurrences of preceeding character, character class or group.'''<br>''' |
| <br> | | <br> | ||
|- | |- | ||
− | | ? | + | | ? |
− | | Matches '''zero '''or '''one '''occurrences of preceeding character, character class or group. | + | | Matches '''zero '''or '''one '''occurrences of preceeding character, character class or group. |
| <br> | | <br> | ||
|- | |- | ||
| () | | () | ||
− | | Parentheses are used for group or capturing group<br> | + | | Parentheses are used for group or capturing group<br> |
| <br> | | <br> | ||
|- | |- | ||
Line 76: | Line 76: | ||
=== Text Patterns and Matches<br> === | === Text Patterns and Matches<br> === | ||
− | <br> | + | A regular expression, or regex for short, is a pattern describing a certain amount of text.<br> |
=== Metacharacters === | === Metacharacters === | ||
− | |||
− | |||
=== Literal Characters<br> === | === Literal Characters<br> === |
Revision as of 14:44, 27 January 2010
Contents |
Quick Reference Table
Regular Expression Pattern | Explanations | Examples |
---|---|---|
Meta characters [\^$.|?*+( |
Special caracters used in regex.
|
|
Literal characters |
All characters (except the metacharacters) match a single instance of themselves.
|
/a/ matches "a" |
[characters] |
Character classes or character set. A character class matches a single character out of all the possibilities offered by the character class. |
/[0-9]/ matches a single digit |
[\d] |
Shorthand character classes matching digits. Same as [0-9]. |
/[\d]/ matches a single digit |
. |
Dot matches any characters. |
/a.c/ matches both "a4c" and "ayc" |
^ | Matches at the start of the string the regex pattern is applied to. Matches a position rather than a character. | |
$ | Matches at the end of the string the regex pattern is applied to. Matches a position rather than a character. | |
{m,n} |
Matches at least “m” and at most “n” occurrences of preceeding character, character class or group. |
|
* | Matches zero or more occurrences of preceeding character, character class or group. | |
+ | Matches one or more occurrences of preceeding character, character class or group. |
|
? | Matches zero or one occurrences of preceeding character, character class or group. | |
() | Parentheses are used for group or capturing group |
|
\0, \1, \2, ... | Substitute the value matched by the nth grouped sub-expression, used in remapped fields. | |
Quick References
Text Patterns and Matches
A regular expression, or regex for short, is a pattern describing a certain amount of text.
Metacharacters
Literal Characters
Character Classes or Character Sets
Shorthand Character Classes
The Dot Matches (Almost) Any Character
Repetition
Optional
Anchors
Alternation
Grouping and Capturing Group
Examples
Here are some examples:
Add 2720 prefix:
/(\d+)/2720\1/
or
/([0-9]*)/2720\1/
Strip first 4 digits:
/([0-9]{4})([0-9]*)/\2/
Strip # and 7 first digits:
/([#])([0-9]{7})([0-9]*)/\3/
Web Online Tools
- Regular builder tool : www.gskinner.com/RegExr
- Ruby regular expression editor and tester : rubular.com
References
- How to Use RegEx in Called and Calling Number Mask
- How to Use RegEx in Remapped Called and Calling Number Mask