Got some text to munge?
Don't hurt your wrists. Use a regular expression!
Groups
- Use $& on the replace side to insert the string you just matched.
- Input: The cat sat, Find: cat, Replace: -$&-, Output: The -cat- sat
- Parenthesis () define a capture group.
Refer to them in Replace with $1 $2 etc, or in Find with \1 \2 etc.
- Input: check the category, Find: (cat)egory, Replace: $1alog, Output: check the catalog
- (cat)\1\1 matches catcatcat
- Parenthesis beginning with question colon (?:) don't affect $1 or \1 etc.
They are useful with quantifiers.
- Input: check the category, Find: (?:cat)egory, Replace: $1alog, Output: check the $1alog
- cat(?:egory)? matches cat or category
- The pipe | matches either what's on its left or what's on its right.
- cat|dog|cow matches cat or dog or cow
Quantifiers
- Curly braces {} define the minimum and maximum times to match whatever precedes them.
- (?:cat){1,2} matches cat or catcat
- Omit the second number to allow infinite matches
- (?:cat){2,} matches two or more repetitions of cat
Shortcut Quantifiers
- ?
- {0,1}
- *
- {0,}
- +
- {1,}
Character Classes
- Square brackets [] define a class of matchable characters.
- [abc] matches a, b, or c.
- Hyphens inside square brackets define ranges.
- [a-zA-Z] matches all lower and upper case letters.
- A caret ^ at the beginning negates the character class.
- [^abc] matches anything but a, b, or c.
Shortcut Character Classes
- .
- Any Character
- \s
- White Space
- \d
- Digit
- \w
- Letter or Digit
- \b
- Word Boundary
- \n
- Newline (enter)
- \t
- Tab
Escaping
- To use a special character literally, precede it with a backslash
- 5\+5\*2=15\? matches 5+5*2=10?
- To use $ in the Replace field, use two $s.
- Input: 5 dollars, Find: (\d+) dollars, Replace: $$$1, Output: $5