regex match any single character (one character only)
Match any single character
- Use the dot
.
character as a wildcard to match any single character.
Example regex: a.c
abc // match
a c // match
azc // match
ac // no match
abbc // no match
Match any specific character in a set
- Use square brackets
[]
to match any characters in a set. - Use
\w
to match any single alphanumeric character:0-9
,a-z
,A-Z
, and_
(underscore). - Use
\d
to match any single digit. - Use
\s
to match any single whitespace character.
Example 1 regex: a[bcd]c
abc // match
acc // match
adc // match
ac // no match
abbc // no match
Example 2 regex: a[0-7]c
a0c // match
a3c // match
a7c // match
a8c // no match
ac // no match
a55c // no match
Match any character except ...
Use the hat in square brackets [^]
to match any single character except for any of the characters that come after the hat ^
.
Example regex: a[^abc]c
aac // no match
abc // no match
acc // no match
a c // match
azc // match
ac // no match
azzc // no match
(Don't confuse the ^
here in [^]
with its other usage as the start of line character: ^
= line start, $
= line end.)
Match any character optionally
Use the optional character ?
after any character to specify zero or one occurrence of that character. Thus, you would use .?
to match any single character optionally.
Example regex: a.?c
abc // match
a c // match
azc // match
ac // match
abbc // no match
See also
- A quick tutorial to teach you the basics of regex
- A practice sandbox to try things out
Simple answer
If you want to match single character, put it inside those brackets [ ]
Examples
- match + ...... [+] or +
- match a ...... a
- match & ...... &
...and so on. You can check your regular expresion online on this site: https://regex101.com/
(updated based on comment)