Difference between revisions of "Regular Expressions"

Jump to navigation Jump to search
4,872 bytes added ,  14:24, 5 June 2013
m
→‎Quantifiers: Typo fixes
m (Added IP address)
 
m (→‎Quantifiers: Typo fixes)
 
(16 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Useful RegEx ==
Most Regular Expression (Regex) implementations are based on or bare similarities to the [[Acronyms#P|PCRE]] library.  The purpose of Regular Expressions are normally to replace or extract bits of data from a larger string.  The syntax of its usage will vary between different languages (eg the functions or constructs that let you use Regex in Perl, PHP, PowerShell, etc), but the syntax of the actual Regex will ''generally'' be consistent.  The secret to creating good Regex is good and thorough testing for things that ''shouldn't'' match as much as ''should'' match.
{|cellpadding="2" cellspacing="0" border="1"  
 
|- style="background-color:#bbddff;"
The following provides a run through of the general syntax (its not exhaustive, its a bit of a work in progress, but contains most of the common stuff).
! Matches                  !! Expression
 
See the [[Regex Examples]] page for some real world examples.
 
== Metacharacters ==
Metacharacters define how an item should match (or not)
{|class="vwikitable"
|-  
! Character !! Meaning !! Example
|-
|-
| '''IP Address'''
| <code>\</code> || Escape
| <code><nowiki> {^\b((25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)\.){3}(25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)\b} </nowiki></code>
|-
| <code>^</code> || Match the beginning of the line || <code>^dog</code> matches <code>dog</code> but not <code>big dog</code>
|-
| <code>$</code> || Match the end of the line || <code>dog$</code> matches <code>dog</code> but not <code>dog run</code>
|-
| <code>.</code> || Match any character || <code>do.</code> matches <code>do</code>, <code>dog</code> but not <code>dogs</code>
|-
| <code>(...)</code> || Grouping, match entire set <code>...</code> ||
|-
| <code>[...]</code> || Bracketed [[#Character_Classes|Character class]], any character contained in set <code>...</code> || <code>[fld]og*</code> matches <code>fog</code>, <code>log</code>, or <code>dog</code> only
|-
| <code><nowiki>|</nowiki></code> || Or, alternation || <code>d<nowiki>|</nowiki>fog</code> matches <code>dog</code> or <code>fog</code>
|-
| <code>?=</code>      || Lookahead                    || <code>dog(?=,)</code> matches the <code>dog</code> in <code>dog,</code> but won't find a match in <code>dog</code>
|-
| <code>?<=</code>      || Look behind                  || <code>(?<=big )dog</code> matches the <code>dog</code> in <code>big dog</code> but won't find a match in <code>dog</code>
|}
 
== Quantifiers ==
Quantifiers define how many times the preceding item should be matched.
{|class="vwikitable"
|-
! Character !! Meaning !! Example
|-
| <code>*</code> || Match 0 or more times || <code>dog*</code> matches <code>do</code>, <code>dog</code>, <code>dogg</code>, or <code>doggg</code>, and so on
|-
| <code>+</code> || Match 1 or more times || <code>dog+</code> matches <code>dog</code>, <code>dogg</code>, or <code>doggg</code>, and so on; but not <code>do</code>
|-
| <code>?</code> || Match 0 or 1 times only || <code>dog?</code> matches <code>do</code> or <code>dog</code> only
|-
| <code>{n}</code> || Match exactly <code>n</code> times || <code>dog{2}</code> matches <code>dogg</code> only
|-
| <code>{n,}</code> || Match <code>n</code> or more times || <code>dog{2,}</code> matches <code>dogg</code>, or <code>doggg</code>, and so on
|-
| <code>{n,m}</code> || Match <code>n</code> up to <code>m</code> times || <code>dog{2,4}</code> matches <code>dogg</code>, <code>doggg</code>, or <code>dogggg</code> only
|}
 
== Character Classes ==
Character Classes define a set of characters that should be matched
{|class="vwikitable"
|-
! Sequence !! Matches !! Example
|-
| <code>[...]</code> || Any character contained in set <code>...</code> || <code>[fld]og*</code> matches <code>fog</code>, <code>log</code>, or <code>dog</code> only
|-
| <code>[:...:]</code> || Any character defined by [[#POSIX Classes|POSIX class]] <code>...</code> ||
|-
| <code>\w</code> || Any word character (alphanumeric and underscore) ||
|-
| <code>\W</code> || Any non-word character ||
|-
| <code>\s</code> || Any white-space character ||
|-
| <code>\S</code> || Any non-whitespace character ||
|-
| <code>\d</code> || Any decimal digit character ||
|-
| <code>\D</code> || Any non-decimal digit character ||
|}
 
=== POSIX Classes ===
[[Acronyms#P|POSIX]] classes are generally not supported by Microsoft implementations.
{|class="vwikitable"
|-
! Class !! Matches !! Equivalent to
|-
| <code>alpha</code> || Any alphabetical character || <code>[A-Za-z]</code>
|-
| <code>alnum</code> || Any alphanumeric character || <code>[A-Za-z0-9]</code>
|-
| <code>ascii</code> || Any [[Acronyms#A|ASCII]] character ||
|-
| <code>blank</code> || A space or tab ||
|-
| <code>cntrl</code> || Control characters ||
|-
| <code>digit</code> || Any decimal digit || <code>[0-9]</code> or <code>\d</code>
|-
| <code>graph</code> || Any graphical/visible character ||
|-
| <code>lower</code> || Any lower-case alphabetical character || <code>[a-z]</code>
|-
| <code>print</code> || Any printable character, including a space ||
|-
| <code>punct</code> || Any punctuation character ||
|-
| <code>space</code> || Any white-space character ||
|-
| <code>upper</code> || Any upper-case character || <code>[A-Z]</code>
|-
| <code>word</code> || Any word character || <code>[A-Za-z0-9_]<\code> or <code>\w</code>
|-
| <code>xdigit</code> || Any hexadecimal digit || <code>[0-9a-fA-F]</code>
|}
|}
[[Category:Regex]]

Navigation menu