IngmarBoddington
2/12/2013 - 5:26 PM

Pearl Compatible Regular Expressions

Pearl Compatible Regular Expressions

-. = Any Single character
-+ = One or more of preceding character
-? = Zero or one of preceding character
-* = One or more of preceding character
-^ = Start of string
-$ = End of string
-| = or
-(Escape any of above with \, for literal \ use \\)
-{n} = n of preceding character
-{n,} = At least n of preceding character
-{,n} = Up to n of preceding character
-{n,m} = Between n and m of preceding character
-\d = Any digit (0-9)
-\D = Inverse of \d
-\w = Any word character (0-9, a-z, A-Z, _)
-\W = Inverse of \w
-\s = Whitespace (space, \n, \t)
-\S = Inverse of \s
-[<characterset>] = Any of character set, can specify ranges e.g. 4-8
-[^<characterset>] = Any not of character set e.g. a-f
-() = Delimit sub-expression (for capturing)
-
-/encloseWithSlashes/
-
-/caseInsensitive/i
-
-/global/g

Useful Regex

Names: /^[a-zA-Z\-\'\s\.]{1,35}$/
Email: /^([a-zA-Z0-9._%-]{1,47}@[a-zA-Z0-9.-]{3,49}){1,50}$/
UK Company Number: /^([A-Z]{2})?[0-9]{8}$/
Date (YYYY-MM-DD): /^2[0-9]{3}\-(0[1-9]|1[0-2])\-(0[1-9]|1[0-9]|2[0-9]|3[0-1])$/
Date (DD-MMM-YYYY): /^(0[1-9]|[1-2][0-9]|3[0-1])\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-\d{4}$/
UK Postcode: ^(GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW]) ?[0-9][ABD-HJLNP-UW-Z]{2})$
UK Sort Code: \d{2}\-?\d{2}\-?\d{2}

#Bloody sed
For extended PCRE (e.g. + character) use -r
[\w] is required by POSIX to match either backslash or w