Special characters with regular expressions and wildcards
Wildcard patterns are written slightly differently than regular expressions.
A wildcard character is a special character that matches one or more other characters. Wildcard patterns use an:
- asterisk (
*
), which matches zero or more of any characters - question mark (
?
), which matches any one character
In regular expressions, instead of ?
, use a period (.
).
For example, the regular expression example.com
matches example.com
, but also exampleacom
, examplebcom
, exampleccom
, etc.
In regular expressions, instead of *
, use .*
. An asterisk (*
) matches only the exact character before it 0 or more times, not 0 or more times of any character. Therefore to achieve the same match as the wildcard pattern, you must use .*
.
For example, the regular expression example*.com
matches exampleeeeee.com
, but does not match example.com
. This is different from a simple wildcard pattern, which would match both. To fix this so that the regular expression matches the same text as a wildcard pattern, the regular expression should be exampl.*\.com
.
Special characters are usually interpreted as a pattern, but you can also match them literally. To match .
or *
, prefix it with the escape character, backslash ( \
) . For example, to match example.com
, use the regular expression example\.com
. For a list of other special characters, see Syntax.