RegEx: Regular Expressions you must know for search and replace


| — A vertical bar separates alternatives. For example, gray|grey can match “gray” or “grey”.

( ) — Parentheses are used to define the scope and precedence of the operators (among other uses). For example, gray|grey and gr(a|e)y are equivalent patterns which both describe the set of “gray” and “grey”.

? — The question mark indicates there is zero or one of the preceding element. For example, colou?r matches both “color” and “colour”.

* — The asterisk indicates there are zero or more of the preceding element. For example, ab*c matches “ac”, “abc”, “abbc”, “abbbc”, and so on.

+ — The plus sign indicates that there is one or more of the preceding element. For example, ab+c matches “abc”, “abbc”, “abbbc”, and so on, but not “ac”.

. — Matches any single character except newlines (exactly which characters are considered newlines is flavor, character encoding, and platform specific, but it is safe to assume that the line feed character is included). Within POSIX bracket expressions, the dot character matches a literal dot. For example, a.c matches “abc”, etc., but [a.c] matches only “a”, “.”, or “c”.

[ ] — A bracket expression. Matches a single character that is contained within the brackets. For example, [abc] matches “a”, “b”, or “c”. [a-z] specifies a range which matches any lowercase letter from “a” to “z”. These forms can be mixed: [abcx-z] matches “a”, “b”, “c”, “x”, “y”, and “z”, as does [a-cx-z]

[^ ] — Matches a single character that is not contained within the brackets. For example, [^abc] matches any character other than “a”, “b”, or “c”. [^a-z] matches any single character that is not a lowercase letter from “a” to “z”. As above, literal characters and ranges can be mixed.

^ — Matches the starting position within the string. In line-based tools, it matches the starting position of any line.

$ — Matches the ending position of the string or the position just before a string-ending newline. In line-based tools, it matches the ending position of any line.

{m,n} — Matches the preceding element at least m and not more than n times. For example, a{3,5} matches only “aaa”, “aaaa”, and “aaaaa”. This is not found in a few, older instances of regular expressions.