Regular expression quick start guide

From TBwiki
(Difference between revisions)
Jump to: navigation, search
Line 4: Line 4:
 
|-
 
|-
 
! align="left" width="250" | Regular Expression Pattern  
 
! align="left" width="250" | Regular Expression Pattern  
! align="left" | Explanations
+
! align="left" | Explanations  
 +
! align="left" | Examples<br>
 +
|-
 +
| &nbsp;&nbsp; [\^$.&#124;?*+(<br>
 +
|
 +
Special caracters used in regex.
 +
 
 +
*Must be escape with backslash "\" to use a literal characters.
 +
 
 +
|
 +
|-
 +
|
 +
Literal characters<br>
 +
 
 +
|
 +
All characters (except the metacharacters) match a single instance of themselves. <br>
 +
 
 +
*{ and } are literal characters, unless they're part of a valid regular expression token (e.g. the {n} quantifier).
 +
 
 +
| ''/a/'' matches "a"<br>
 +
|-
 +
| <br>
 +
| <br>
 +
| <br>
 
|-
 
|-
 
| ^  
 
| ^  
| Matches beginning of a line
+
| Matches beginning of a line  
 +
| <br>
 
|-
 
|-
 
| $  
 
| $  
| Matches the end of a line
+
| Matches the end of a line  
 +
| <br>
 
|-
 
|-
 
| \d  
 
| \d  
| Matches a digit
+
| Matches a digit  
 +
| <br>
 
|-
 
|-
 
| [characters]  
 
| [characters]  
| Matches any single character between the brackets
+
| Matches any single character between the brackets  
 +
| <br>
 
|-
 
|-
 
| re1&#124;re2  
 
| re1&#124;re2  
| Match either re1 or re2
+
| Match either re1 or re2  
 +
| <br>
 
|-
 
|-
 
| re*  
 
| re*  
| Matches zero or more occurrences of re
+
| Matches zero or more occurrences of re  
 +
| <br>
 
|-
 
|-
 
| re+  
 
| re+  
| Matches one or more occurrences of re
+
| Matches one or more occurrences of re  
 +
| <br>
 
|-
 
|-
 
| re?  
 
| re?  
| Matches zero or one occurrences of re
+
| Matches zero or one occurrences of re  
 +
| <br>
 
|-
 
|-
 
| Re{m,n}  
 
| Re{m,n}  
| Matches at least “m” and at most “n” occurrences of re
+
| Matches at least “m” and at most “n” occurrences of re  
 +
| <br>
 
|-
 
|-
 
| (...)  
 
| (...)  
| Parentheses are used to group regular expressions
+
| Parentheses are used to group regular expressions  
 +
| <br>
 
|-
 
|-
 
| \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>
 
|}
 
|}
  
Line 47: Line 81:
  
 
<br>  
 
<br>  
 +
 +
=== Metacharacters ===
 +
 +
===  ===
  
 
=== Literal Characters<br>  ===
 
=== Literal Characters<br>  ===
Line 104: Line 142:
 
  /([#])([0-9]{7})([0-9]*)/\3/
 
  /([#])([0-9]{7})([0-9]*)/\3/
  
== Online Tools  ==
+
== Web Online Tools  ==
  
*Regex builder tool :&nbsp;[http://www.gskinner.com/RegExr www.gskinner.com/RegExr]
+
*Regular builder tool&nbsp;:&nbsp;[http://www.gskinner.com/RegExr www.gskinner.com/RegExr]  
*Ruby regular expression editor and tester : [http://rubular.com rubular.com]
+
*Ruby regular expression editor and tester&nbsp;: [http://rubular.com rubular.com]<br>
  
 
== References  ==
 
== References  ==
Line 113: Line 151:
 
*[[Toolpack: How to Use RegEx in Called and Calling Number Mask|How to Use RegEx in Called and Calling Number Mask]]  
 
*[[Toolpack: How to Use RegEx in Called and Calling Number Mask|How to Use RegEx in Called and Calling Number Mask]]  
 
*[[Toolpack: How to Use RegEx in Remapped Called and Calling Number Mask|How to Use RegEx in Remapped Called and Calling Number Mask]]
 
*[[Toolpack: How to Use RegEx in Remapped Called and Calling Number Mask|How to Use RegEx in Remapped Called and Calling Number Mask]]
 +
 +
<br>

Revision as of 17:36, 26 January 2010

Contents

Quick Reference Table

Regular Expression Pattern Explanations Examples
   [\^$.|?*+(

Special caracters used in regex.

  • Must be escape with backslash "\" to use a literal characters.

Literal characters

All characters (except the metacharacters) match a single instance of themselves.

  • { and } are literal characters, unless they're part of a valid regular expression token (e.g. the {n} quantifier).
/a/ matches "a"



^ Matches beginning of a line
$ Matches the end of a line
\d Matches a digit
[characters] Matches any single character between the brackets
re1|re2 Match either re1 or re2
re* Matches zero or more occurrences of re
re+ Matches one or more occurrences of re
re? Matches zero or one occurrences of re
Re{m,n} Matches at least “m” and at most “n” occurrences of re
(...) Parentheses are used to group regular expressions
\0, \1, \2, ... Substitute the value matched by the nth grouped sub-expression, used in remapped fields.


Quick References

Text Patterns and Matches


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

References


Personal tools