Special characters with regular expressions and wildcards
A wildcard character is a special character that represents one or more other characters. The most commonly used wildcard characters are the asterisk (*
), which typically represents zero or more characters, and the question mark (?
), which typically represents any one character.
In Perl-style regular expressions, the period (.
) character refers to any single character. It is similar to the question mark (?)
character in wildcard match pattern. As a result, example.com not only matches example.com but also exampleacom, examplebcom, exampleccom, and so forth.
To match a special character such as “.
” and “*
” use the backslash ( \
) escape character. For example, to match example.com, the regular expression should be: example\.com
In Perl regular expressions, an asterisk (*
) matches the character before it 0 or more times, not 0 or more times of any character. For example, example*.com
matches exampleeeeee.com
but does not match example.com
.
To match any character 0 or more times, use “.*
” where “.
” means any character and the “*
” means 0 or more times. For example, the wildcard match pattern exampl*.com
should therefore be exampl.*\.com
.
To match any single character within a range of characters, enclose the character-set in square brackets ([ ]
). For example, [A-Za-z]
matches any single uppercase or lowercase letter.