Regular Expression Notes

Replacement Text Reference
RegEx in a Nutshell

Regular expression for man pages to remove bullshit.

.

Grouping & Modifiers

Capturing Group

(regex)

Non-Capturing Group

(?:regex)

Atomic Group

(?>regex) Prevents backtracking back into the group after a match.

LookAround

Positive Lookahead: (?=RegEx)
Negative Lookahead: (?!RegEx)
Positive Lookbehind: (?<=RegEx)
Negative Lookbehind: (?<!RegEx)

([0-9]{3})[-)./ ]*([0-9]{3})[-)./ ]*([0-9]{4})
$1-$2-$3

([0-9]{3})\D*([0-9]{3})\D([0-9]{4})
$1-$2-$3

EXPLANATION: Reformat all phone numbers to 123-456-7890
([0-9]{3})	Three numbers, capture
[-)./ ]*	May or may not have hyphen(s), closing parenthesis, period(s), slash(es)  or space(s)
([0-9]{3})	Three numbers, capture
[-./ ]*		May or may not have hyphen(s), period(s), slash(es)  or space(s)
([0-9]{4})	Four numbers, capture

Conditionals

(?(?=regex)then|else)

IF lookaround (and) THEN RegEx ELSE RegEx.

Success if the lookaround and THEN match or the lookaround fails and the ELSE matches

TextMate Conditionals

(group)?
(?IF:THEN:ELSE)

Lowercase and replace spaces with a hyphen

([^ ]+)( )?
\L$1\E(?2:-)

RegEx Comment

(?#comment)

Turn Case-Insensitivity on or off for the Rest of the Expression

(?i) Toggle On
(?-i) Toggle Off