Forked from ccstone/BBEdit-TextWrangler_RegEx_Cheat_Sheet.txt
Last active
August 29, 2015 14:07
-
-
Save professordino/9b0d75c1e36ae63860ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
BBEDIT/TEXTWRANGLER REGULAR EXPRESSION GUIDE MODIFIED 2014-01-13 : 00:12 | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
NOTES: | |
The PCRE engine (Perl Compatible Regular Expressions) is what BBEdit and TextWrangler use. | |
Items I'm unsure of are marked '# PCRE?'. The list while fairly comprehensive is not complete. | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
PATTERN MODIFIERS (switches) | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
i Case-insensitive | |
m Multiline : allow the grep engine to match at ^ and $ after and before at \r or \n. | |
s Magic Dot : allows . to match \r and \n | |
x Free-spacing: ignore unescaped white space; allow inline comments in grep patterns. | |
(?imsx) On | |
(?-imsx) Off | |
(?i-msx) Mixed | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
Regex Meta-Characters: | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
. Any character except newline or carriage return | |
[ ] Any single character of set | |
[^ ] Any single character NOT of set | |
* 0 or more previous regular expression | |
*? 0 or more previous regular expression (non-greedy) | |
+ 1 or more previous regular expression | |
+? 1 or more previous regular expression (non-greedy) | |
? 0 or 1 previous regular expression | |
| Alternation | |
( ) Grouping regular expressions | |
^ Beginning of a line or string | |
$ End of a line or string | |
{m,n} At least m but most n previous regular expression | |
{m,n}? At least m but most n previous regular expression (non-greedy) | |
\1-9 Nth previous captured group | |
\& Whole match # BBEdit: '&' only - no escape needed | |
\` Pre-match # PCRE? NOT BBEdit | |
\' Post-match # PCRE? NOT BBEdit | |
\+ Highest group matched # PCRE? NOT BBEdit | |
\A Beginning of a string | |
\b Backspace(0x08)(inside[]only) # PCRE? | |
\b Word boundary(outside[]only) | |
\B Non-word boundary | |
\d Digit, same as[0-9] | |
\D Non-digit | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
Case-Change Operators | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
\E Change case - acts as an end delimiter to terminate runs of \L & \U. | |
\l Change case of only the first character to the right lower case. (Note: lowercase 'L') | |
\L Change case of all text to the right to lowercase. | |
\u Change case of only the first character to the right to uppercase. | |
\U Change case of all text to the right to uppercase. | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
White-Space or Non-White-Space | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
\t Tab | |
\n Linefeed | |
\r Return | |
\f Formfeed | |
\s Whitespace character equivalent to [ \t\n\r\f] | |
\S Non-whitespace character | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
\W Non-word character | |
\w Word character[0-9A-Za-z_] | |
\z End of a string | |
\Z End of a string, or before newline at the end | |
(?#) Comment | |
(?:) Grouping without backreferences | |
(?=) Zero-width positive look-ahead assertion | |
(?!) Zero-width negative look-ahead assertion | |
(?>) Nested anchored sub-regexp stops backtracking | |
(?imx-imx) Turns on/off imx options for rest of regexp | |
(?imx-imx:β¦) Turns on/off imx options, localized in group # 'β¦' indicates added regex pattern | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
PERL-STYLE PATTERN EXTENSIONS : BBEdit Documentation : 'β¦' indicates added regex pattern | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
Extension Meaning | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
(?:β¦) Cluster-only parentheses, no capturing | |
(?#β¦) Comment, discard all text between the parentheses | |
(?imsx-imsx) Enable/disable pattern modifiers | |
(?imsx-imsx:β¦) Cluster-only parens with modifiers | |
(?=β¦) Positive lookahead assertion | |
(?!β¦) Negative lookahead assertion | |
(?<=β¦) Positive lookbehind assertion | |
(?<!β¦) Negative lookbehind assertion | |
(?()β¦|β¦) Match with if-then-else | |
(?()β¦) Match with if-then | |
(?>β¦) Match non-backtracking subpattern (βonce-onlyβ) | |
(?R) Recursive pattern | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
POSITIONAL ASSERTIONS (duplicatation of above) | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
POSITIVE LOOKAHEAD ASSERTION: (?='pattern') | |
NEGATIVE LOOKAHEAD ASSERTION: (?!'pattern') | |
POSITIVE LOOKBEHIND ASSERTION: (?<='pattern') # Lookbehind Assertions are of fixed-length | |
NEGATIVE LOOKBEHIND ASSERTION: (?<!'pattern') | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
SPECIAL CHARACTER CLASSES (POSIX standard except where 'Perl Extension' is indicated): | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
CLASS MEANING | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
[[:alnum:]] Alpha-numeric characters | |
[[:alpha:]] Alphabetic characters | |
[[:ascii:]] Character codes 0-127 # Perl Extension | |
[[:blank:]] Horizontal whitespace | |
[[:cntrl:]] Control characters | |
[[:digit:]] Decimal digits (same as \d) | |
[[:graph:]] Printing characters, excluding spaces | |
[[:lower:]] Lower case letters | |
[[:print:]] Printing characters, including spaces | |
[[:punct:]] Punctuation characters | |
[[:space:]] White space (same as \s) | |
[[:upper:]] Upper case letters | |
[[:word:]] Word characters (same as \w) # Perl Extension | |
[[:xdigit:]] Hexadecimal digits | |
Usage example of multiple character classes: | |
[[:alpha:][:digit:]] | |
Β«NegatedΒ» character class example: | |
[[:^digit:]]+ | |
** POSIX-style character class names are case-sensitive | |
** The outermost brackets above indicate a RANGE; the class name itself looks like this: [:alnum:] | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
CONDITIONAL SUBPATTERNS | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
Conditional subpatterns allow you to apply βif-thenβ or βif-then-elseβ logic to pattern matching. | |
The βifβ portion can either be an integer between 1 and 99, or an assertion. | |
The forms of syntax for an ordinary conditional subpattern are: | |
if-then: (?(condition)yes-pattern) | |
if-then-else: (?(condition)yes-pattern|no-pattern) | |
and for a named conditional subpattern are: | |
if-then: (?P<NAME>(condition)yes-pattern) | |
if-then-else: (?P<NAME>(condition)yes-pattern|no-pattern) | |
If the condition evaluates as true, the βyes-patternβ portion attempts to match. Otherwise, the | |
βno-patternβ portion does (if there is a βno-patternβ). | |
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment