Regular Expressions Cheatsheet (PCRE)
|
|
|
Php Regex Cheat Sheet
This guide is based on the information provided by
Php regex cheat sheet. A simple PHP regex cheat sheet via, PHP PCRE Cheat Sheet. Functions pregmatch(pattern, subject, submatches) pregmatchall(pattern, subject, submatches) pregreplace(pattern PHP PCRE Cheat Sheet Functions pregmatch(pattern, subject, submatches) replacement as PHP code S Extra analysis of pattern U Pattern is ungreedy.
Last modified: Tuesday, January 24, 2017 | Copyright © 2010-2017 OpenSight Software, LLC |
- Title: PHP PCRE Cheat Sheet Author: Richard Heyes Created Date: 9/14/2005 9:45:10 PM.
- PHP PCRE Cheat Sheet. Functions Base Character Classes pregmatch(pattern, subject, submatches) w Any “word” character (a-z 0-9 ) pregmatchall(pattern, subject, submatches) W Any non “word” character pregreplace(pattern, replacement, subject) s Whitespace (space, tab CRLF) S Any non whitepsace character pregreplacecallback(pattern, callback, subject) d Digits (0-9) preg.
Although similar to Perl, PCRE has some syntax that can bedifficult to remember. When I'm working with CFEngine I often have tolook it up. Here is a cheat-sheet to save time.
Description | Syntax | Note |
---|---|---|
Case insensitive | (?i) | Place at the beginning of the expression. |
Class | [ ] | d and [0-9-] are equivalent. |
Digit | d | D for anything that is not a digit. |
Lookahead, negative | (?!*pattern* | |
Lookahead, positive | (?=*pattern* | |
Lookbehind, negative | (?<!*pattern* | |
Lookbehind, positive | (?<=*pattern* | |
None capture grouping | (?:)Group for logic and selection, but not capture. | |
Multi-line match | (?m) | Place at the beginning of the expression. Similar to Perl's m//g. |
Extened regex | (mxs) | Use this to make your regexes readable. A regex best practice. |
No magic | Q E | No special meaning to any characters between these. |
Range | {n,m} | Minimum of *n*, maximum of *m*. |
Whitespace | s | S for anything that is not whitespace. |
Notes
Beginning settings are combined. For example (?mi) considersmultiple lines and is case insensitive.
Make your regexes readable by using the extened regex options.Consider this:
(?i)(?:linux|solaris|aix|hpux)
Versus this:
(?imsx)(?: linux | solaris | aix | hpux )
Pcre Regex Cheat Sheet
Now imagine using it on a very complex regex.See here fora more complex example.