Basic Regex Operators

Regex uses various operators to define search patterns. The following table shows some regex operators and examples of each. This list is provided for your convenience only. CloudAdvisor supports all Google RE2 operators, even if they do not appear in the following table.

Operator Usage Example
Vertical bar or Pipe ( | ) Boolean OR. x|y matches "x" or "y".
Question mark ( ? ) Indicates zero or one of the preceding element. colou?r matches both "color" and "colour".
Asterisk ( * ) Indicates zero or more of the preceding element. ab*c matches "ac", "abc", "abbc", "abbbc", and so on.
Plus sign ( + ) Indicates one or more of the preceding element. ab+c matches "abc", "abbc", "abbbc", and so on; Does not match "ac".
{min,max} Matches between min and max occurrences of the preceding character, class, or subpattern. a{1,2} matches "ab", but only the first two a's in "aaab".
Square brackets [...]

Identify classes of characters. The square brackets enclose a list or range of characters (or both).

Using a dash in between values specifies a range.

You can combine lists and ranges.

[abc] matches any single character that is either "a", "b", or "c".

[a-z] matches "any single character that is between lowercase a and z (inclusive)".

[a-zA-Z0-9_] matches any single character that is alphanumeric or an underscore.

Caret/circumflex in square brackets [^...] Matches any single character that is not in the class.

[^/]* matches zero or more occurrences of any character that is not a forward-slash.

[^0-9xyz] matches any single character that is not a digit and is not the letter x, y, or z.

\d Matches any single digit (equivalent to the class [0-9]). [\d.-] matches any single digit, period, or minus sign.
\D Matches any single non-digit (anything that is not a digit; 0-9). [\D.-] matches any single non-digit, period, or minus sign.
\s Matches any single white space character (for example, spaces, tabs, and newlines [`r and `n]). \s matches any single white space characters.
\S Matches any non-white space character. \S matches any single non-white space characters (letters, numbers, symbols, and so on).
\w Matches any single "word" character (alphanumeric or underscore). Equivalent to [a-zA-Z0-9_]. \w matches any single "word" characters. In the sentence, "Are you there?", the regex would match all of the letters in the words "Are", "you", and "there", but would not match the spaces or punctuation.
\W Matches any non-word character. \W matches any single "non-word" characters. In the sentence, "Yes, I am here!", the regex would match all of the spaces and the punctuation.
Caret or circumflex ( ^ ) Anchor; ties the pattern to the beginning of the string being searched, where no values can occur before the string. ^abc matches "abc123", but not "123abc".
Dollar sign ( $ ) Anchor; ties the pattern to the end of the string being searched, where no values can occur after the string. abc$ matches "123abc", but not "abc123".
\b

Word boundary; requires the current character status as a word character (\w) to be the opposite of that of the previous character.

Typically used to avoid accidentally matching a word that appears inside another word.

\bcat\b matches "cat", regardless of the punctuation and white space surrounding it, but does not match "catfish" or "tomcat".
\B Not a word boundary; requires that the current character not appear at a word boundary. \Bcat\B matches "concatenate", but does not match "cat", "catfish", or "tomcat".
Parentheses (...)

Group; defines scope and precedence.

Enables you to apply *, ?, +, or {min,max} to a series of characters rather than just one.

gr(e|a)y matches "grey" and "gray".

(Sun|Mon|Tues|Wednes|Thurs|Fri|Satur)day matches the name of any day of the week.

(abc)+ matches one or more occurrences of the string "abc"; it matches "abcabc123", but not "ab123" or "bc123".

(?im) turns on the case-insensitive (i) and multiline (m) options for the remainder of the pattern. Typically used within regex patterns.

(?-im) turns off the case-insensitive (i) and multiline (m) options for the remainder of the pattern. Typically used within regex patterns.

Question mark and colon in a group ( ?: )

Defines a non-capturing group. The group is used to delimit the tag match but the text is not part of the tag match. For more information, see Capture Groups.

(?:\W) matches any non-word characters, but the match to this group is not part of the tag match.