At a very basic level the pattern means "contains"
Basic Examples
- a|b* denotes { "a", "b", "bb", "bbb", ...}
- (a|b)* denotes the set of all strings with no symbols other than "a" and "b", including the empty string: {"a", "b", "aa", "ab", "ba", "bb", "aaa", ...}
- ab*(c|ε) denotes the set of strings starting with "a", then zero or more "b"s and finally optionally a "c": {"a", "ac", "ab", "abc", "abb", "abbc", ...}
- (0|(1(01*0)*1))* denotes the set of binary numbers that are multiples of 3: { "0", "00", "11", "000", "011", "110", "0000", "0011", "0110", "1001", "1100", "1111", "00000", ... }
Advanced Examples
- .at matches any three-character string ending with "at", including "hat", "cat", and "bat".
- [hc]at matches "hat" and "cat".
- [^b]at matches all strings matched by .at except "bat".
- [^hc]at matches all strings matched by .at other than "hat" and "cat".
- ^[hc]at matches "hat" and "cat", but only at the beginning of the string or line.
- [hc]at$ matches "hat" and "cat", but only at the end of the string or line.
- \[.\] matches any single character surrounded by "[" and "]" since the brackets are escaped, for example: "[a]" and "[b]".
- s.* matches any number of characters preceded by s, for example: "saw" and "seed".
Negative Look-ahead
Sometimes you want to do some special regex that DOES NOT contain certain patterns but also DOES contain others. To do this you need the negative look-around operator (?...)
- ^(?:(?!foobar).)*$ does not contain foobar
- ^foo-(?:(?!bar).)*$ starts with foo does not contain bar
- ^(?:(?!__).)*$ does not contain '__' - useful for matching device hostname only and no sub-elements
Special Exclusion Example
- (?=^((?!foobar).)*$). does not contain foobar anywhere in the whole string