Crimson Editor

Regular Expressions

Crimson Editor’s Find and Replace (the Search menu) can match regular expressions — text patterns that describe what to look for instead of an exact string. Turn on the Regular expression option in the Find or Replace dialog, then build your pattern from the special characters below.

For example, [0-9] finds any single digit, and ^#include finds every line that starts with #include. To match a special character literally, put a backslash in front of it — \* matches a real asterisk.

Special characters

CharacterMeaning
^Start of the line. ^A matches an “A” only at the beginning of a line.
$End of the line. abc$ matches “abc” only at the end of a line.
.Any single character.
*The preceding item, 0 or more times.
+The preceding item, 1 or more times (at least once).
?The preceding item, 0 or 1 time.
|Alternation — either side may match. a|b matches “a” or “b”.
[ … ]A character class — any one of the enclosed characters matches. Use - for a range, e.g. [0-9] or [a-z].
[^ … ]A caret just after [ negates the class. [^0-9] matches any character that is not a digit.
( … )Groups a sub-pattern, and captures it as a tagged expression for the replacement (see below).
\Escape — match the following special character literally. \. matches a real dot.

Tagged expressions (replace)

Parentheses do more than group — each ( … ) also remembers the text it matched, so you can reuse it in the Replace field:

  • & or \0 — the entire matched text.
  • \1, \2, \3, … — the text captured by the 1st, 2nd, 3rd… set of parentheses.

So searching for (Mr)(\.) and replacing with \1s\2 turns Mr. into Mrs.\1 is “Mr”, \2 is ”.”, with an “s” inserted between them.

Examples

TextSearchReplaceResult
Mr.(Mr)(\.)\1s\2Mrs.
abc(a)b(c)&-\1-\2abc-a-c
bcd(a|b)c*d&-\1bcd-b
abcde(.*)c(.*)&-\1-\2abcde-ab-de
cde(ab|cd)e&-\1cde-cd

Crimson Editor’s regular-expression engine is based on Henry Spencer’s library.