PCRE (Perl Compatible Regular Expressions) is a C library implementing regex. It was written in 1997 when Perl was the de-facto choice for complex text processing tasks. The syntax for patterns used in PCRE closely resembles Perl. PCRE syntax is being used in many big projects called flavors – .NET, Java, JavaScript, XRegExp, Perl, PCRE, Python, and Ruby, and the programming languages C#, Java, JavaScript, Perl, PHP, Python, Ruby, and VB.NET.
PCRE’s syntax is much more powerful and flexible than either of the POSIX regular expression flavors and than that of many other regular-expression libraries.
we’re focused on PRCE mostly unless stated!
Posix Regex Examples
AnchorsStart of string, or start of line in multi-line pattern | Start of string | End of string, or end of line in multi-line pattern | End of string | Word boundary | Not word boundary | Start of word | End of word |
Character ClassesControl character | White space | Not white space | Digit | Not digit | Word | Not word | Hexadecimal digit | Octal digit |
POSIXUpper case letters | Lower case letters | All letters | Digits and letters | Digits | Hexadecimal digits | Punctuation | Space and tab | Blank characters | Control characters | Printed characters | Printed characters and spaces | Digits, letters and underscore |
AssertionsLookahead assertion | Negative lookahead | Lookbehind assertion | Negative lookbehind | Once-only Subexpression | Condition [if then] | Condition [if then else] | Comment |
Groups and RangesAny character except new line (n) | a or b | Group | Passive (non-capturing) group | Range (a or b or c) | Not (a or b or c) | Lower case letter from a to q | Upper case letter from A to Q | Digit from 0 to 7 | Group/subpattern number “x” |
Pattern ModifiersGlobal match | Case-insensitive | Multiple lines | Treat string as single line | Allow comments and whitespace in pattern | Evaluate replacement | Ungreedy pattern |
String Replacementnth non-passive group | “xyz” in /^(abc(xyz))$/ | “xyz” in /^(?:abc)(xyz)$/ | Before matched string | After matched string | Last matched string | Entire matched string |
Hidden chars or shortcutss = [ tnrf] d = [0-9] w = [a-zA-Z_0-9]) |
|
Quantifiers0 or more | Exactly 3 | 1 or more | 3 or more | 0 or 1 | 3, 4 or 5 |
Escape SequencesEscape following character | Begin literal sequence | End literal sequence |
“Escaping” is a way of treating characters which have a special meaning in regular expressions literally, rather than as special characters. Special CharactersNew line | Carriage return | Tab | Vertical tab | Form feed | Octal character xxx | Hex character hh |
Case Conversionl Make next character lowercase u Make next character uppercase L Make entire string (up to E) lowercase U Make entire string (up to E) uppercase uL Capitalize first char, lowercase rest (sentence) |
PCRE regex quick reference
[abx-z] | One character of: a, b, or the range x-z |
[^abx-z] | One character except: a, b, or the range x-z |
a|b | a or b |
a? | Zero or one a’s (greedy) |
a?? | Zero or one a’s (lazy) |
a* | Zero or more a’s (greedy) |
a*? | Zero or more a’s (lazy) |
a+ | One or more a’s (greedy) |
a+? | One or more a’s (lazy) |
a{4} | Exactly 4 a’s |
a{4,8} | Between (inclusive) 4 and 8 a’s |
a{9,} | 9 or more a’s |
(?>…) | An atomic group |
(?=…) | A positive lookahead |
(?!…) | A negative lookahead |
(?<=…) | A positive lookbehind |
(?<!…) | A negative lookbehind |
(?:…) | A non-capturing group |
(…) | A capturing group |
(?P<n>…) | A capturing group named n |
Posix Regex Cheat Sheet 2019
POSIX PO pattern. Post Posting Guidelines. Match an email address Validate an ip address date format (yyyy-mm-dd) Url Validation Regex Regular Expression - Taha match whole word Match or Validate phone number nginx test Match html tag Blocking site with unblocked games. Character classes. Any character except. Regular Expressions Enjoy this cheat sheet at its fullest within Dash. Some regex implementations use instead of $. POSIX :upper: Uppercase letters.
Perl Regex Cheat Sheet Pdf
^ | Beginning of the string |
$ | End of the string |
d | A digit (same as [0-9]) |
D | A non-digit (same as [^0-9]) |
w | A word character (same as [_a-zA-Z0-9]) |
W | A non-word character (same as [^_a-zA-Z0-9]) |
s | A whitespace character |
S | A non-whitespace character |
b | A word boundary |
B | A non-word boundary |
n | A newline |
t | A tab |
cY | The control character with the hex code Y |
xYY | The character with the hex code YY |
uYYYY | The character with the hex code YYYY |
. | Any character |
Y | The Y’th captured group |
(?1) | Recurse into numbered group 1 |
(?&x) | Recurse into named group x |
(?P=n) | The captured group named ‘n’ |
(?#…) | A comment |