Regular expression quick start guide
From TBwiki
(Difference between revisions)
m (Added Lumadis regex tester (includes replace option)) |
(Added NOT operator) |
||
| (3 intermediate revisions by one user not shown) | |||
| Line 67: | Line 67: | ||
| \0, \1, \2, ... | | \0, \1, \2, ... | ||
| Substitute the value matched by the nth grouped sub-expression, used in remapped fields. | | Substitute the value matched by the nth grouped sub-expression, used in remapped fields. | ||
| + | | <br> | ||
| + | |- | ||
| + | | ?! | ||
| + | | Not, as in "everything except this". | ||
| <br> | | <br> | ||
|} | |} | ||
| Line 92: | Line 96: | ||
== Web Online Tools == | == Web Online Tools == | ||
| − | *Regular builder tool : [http://www.gskinner.com/RegExr www.gskinner.com/RegExr] | + | *Regular builder tool (with replace) : [http://www.gskinner.com/RegExr www.gskinner.com/RegExr] |
| − | + | Tips to use: | |
| − | *Ruby regular expression editor and tester | + | # No need to enclose the Regular expression with '/'. |
| + | # Replace : the regular expression must be split in two parts and '\' replaced by '$'. For example, with '/^([0-9]*)$/2720\1/', the '^([0-9]*)$' would be filled on the first line and '2720$1' on the second line. | ||
| + | |||
| + | *Ruby regular expression editor and tester : [http://rubular.com rubular.com]<br> | ||
== References == | == References == | ||
Latest revision as of 10:04, 31 January 2014
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. | |
| ?! | Not, as in "everything except this". | |
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 (with replace) : www.gskinner.com/RegExr
Tips to use:
- No need to enclose the Regular expression with '/'.
- Replace : the regular expression must be split in two parts and '\' replaced by '$'. For example, with '/^([0-9]*)$/2720\1/', the '^([0-9]*)$' would be filled on the first line and '2720$1' on the second line.
- Ruby regular expression editor and tester : rubular.com
References