BBEdit Grep Reference
BBEdit Grep Reference / Cheat Sheet
Pulled from this PDF on BareBones website
A character in a grep pattern generally matches itself. Metacharacters (summarized in the Primitives table) must be preceded by backslash ( \ ) to match literally.
PRIMITIVES
| \A | 0 chars at start of file | \z | …at end of file |
| --- | --- | --- | --- |
| \Z | 0 chars at end of file or in front of newline immediately before eof | | |
| ^ | 0 chars at start of line | $ | …at end of line |
| \b | 0 chars at word boundary | \B | …not at a word boundary |
| . | dot/period matches any 1 character except newline, but see (?s) | | |
| \ \ \^ \ . | match literal backslash, caret, period; backslash escape is also necessary to match $ ( ) * ? { } [ ] | + | | |
| \t | tab; equivalently, \x09 | | |
| \n | newline/“linefeed ” | \r | |
| \0 377 | (backslash, zero) octal code [deprecated, use hex codes instead] | | |
| \xff | hexadecimal code | \x{ffff} | hexadecimal code |
| \s | whitespace character | \S | non-whitespace character |
| \w | word character, equivalent to [a-zA-Z0-9etc] | \W | non-word character (incl. newline) |
| \d | digit | \D | nondigit (incl. newline) |
| [abc] | any single character in set (see also Posix classes) | [a-z] | any single character in specified range |
| [^abcx-z] | any single character (possibly \n) *not* in specified characters or range[s] | | |
A character class [abc] matches exactly one character, but [abc] may be qualified by +, *, ?, {n} etc. A‑Z indicates a range. Within a character class, characters such as .*| match themselves with no special interpretation. To match a hyphen, place it first or backslash-escape it. Backslash escape \] matches ], \\ matches \
POSIX CHARACTER CLASSES
Match 1 char within a character class set
| [:alnum:] | alphanumeric |
| --- | --- |
| [:alpha:] | including Unicode |
| [:ascii:] | [\x00-\x7f] |
| [:blank:] | horiz whitespace |
| [:cntrl:] | control codes |
| [:digit:] | [0-9], same as \d |
| [:graph:] | printable not spaces |
| [:lower:] | lowercase letter |
| [:print:] | printable incl spaces |
| [:punct:] | punctuation |
| [:space:] | same as \s |
| [:upper:] | uppercase letter |
| [:word:] | same as \w |
| [:xdigit:] | hex digit [0-9A-Fa-f] |
| [:^class:] | invert the class |
PATTERN MODIFIERS (?on-off )
| (?i) | case insensitive |
| --- | --- |
| (?m) | multiline: ^ and $ match begin/end of file |
| (?s) | . matches newline, but consider (.|\n) instead |
| (?x) | ignore whitespace; allow # comments |
ALTERNATIVES/SUBPATTERNS
| p|q | alternation, match p or match q; also, | p|q|r|s | |
| --- | --- | --- | --- |
| (p) | subpattern (capture) | \1
.. \99 | backreference to subpattern capture |
| (?:p) | subpattern (noncapture / cluster-only) | | |
| (?P<a>) | name a for subpattern p (named capture) | (?P=a) | backreference |
QUANTIFIERS
| p? | zero or one (greedy) | p?? | zero or one (nongreedy) |
| --- | --- | --- | --- |
| p* | zero or more (greedy) | p*? | zero or more (nongreedy) |
| p+ | one or more (greedy) | p+? | one or more (nongreedy) |
| p{n} | exactly n (greedy) | p{n}? | exactly n (nongreedy) |
| p{m,} | at least m (greedy) | p{m,}? | at least m (nongreedy) |
| p{m,n} | at least m, but no more than n (greedy) | p{m,n}? | at least m, but no more than n (nongreedy) |
POSITIONAL ASSERTIONS match 0 characters
| (?<!x) | negative lookbehind: match subpattern only if not preceded by x | (?!x) | negative lookahead: match subpattern only if not followed by x |
| --- | --- | --- | --- |
| (?<=x) | positive lookbehind: match subpattern only if preceded by x | (?=x) | positive lookahead: match subpattern only if followed by x |
CONDITIONAL SUBPATTERNS
| (?(k)y) | if subpattern number k matched, attempt match of y-clause, else, skip; for named subpattern, (?(P<a>)y) |
| --- | --- |
| (?(k)y|n) | if subpattern number k matched, attempt match of y-clause; else, attempt match of n-clause |
ONCE-ONLY SUBPATTERNS (NONCAPTURING)
| (?>p) | If p matches, prevent backtracking in p |
| --- | --- |
COMMENT
in (? x) mode, # starts comment until and including newline; also …
| (?# comment) | enclosed text is ignored, whether or not x‑mode is set |
| --- | --- |
REPLACEMENT PATTERN
escape is backslash in BBEdit; other systems may use \ or $
| & or \0 | entire match |
| --- | --- |
| \1.. \99 | k $th$ subpattern |
| \P<a> | named subpattern |
| \u | case specifier: make |
| \ℓ | next character upper/lowercase |
| \U | make characters |
| \L | upper/lowercase until next case specifier |
| \E | end case transform |