76

How do you match any one character with a regular expression?

A number of other questions on Stack Overflow sound like they promise a quick answer, but they are actually asking something more specific:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • Yes, the search engines are not good at this (e.g., due to titles that are too general (unspecific)). Here is another one (it is really exactly one ***alphabetic*** character): *[Regular expression for one character only](https://stackoverflow.com/questions/15928216/regular-expression-for-one-character-only)* – Peter Mortensen Jul 02 '22 at 13:23
  • Here is a similar basic canonical question, for which the answer is `.*`: *[Symbol for any number of any characters in regex?](https://stackoverflow.com/questions/6441015/)* – Peter Mortensen Jul 02 '22 at 13:27

3 Answers3

101

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

Max
  • 915
  • 10
  • 28
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • 1
    This does not work if we need to match any single leading character. For example `.bc` matches `abc` and `aabc` as well. How can we require just one character in this case? – Dinko Ivanov Dec 08 '18 at 18:29
  • 2
    @DinkoIvanov `\b.{1}bc\b` will do that. `{1}` stands for no more and no less than one `\b` is for word boundry. – Kolja Feb 10 '20 at 07:19
  • this should be a solution rather than n guide – juxhin bleta Feb 07 '23 at 14:40
5

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)

Daniel Perník
  • 5,464
  • 2
  • 38
  • 46
  • 1
    The brackets are not required. You can confirm that at the site you linked to. The `+` character, since it has a special meaning, can be searched by escaping it with a backslash: `\+`. – Suragch Jan 21 '19 at 17:10
2

If you are searching for a single isolated character or a set of isolated characters within any string you can use this

\b[a-zA-Z]\s

this will find all single english characters in the string

similarly use

\b[0-9]\s

to find single digits like it will pick 9 but not 98 and so on

Rafey Rana
  • 41
  • 1