Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex match any single character (one character only)

Tags:

regex

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

I am writing this question and the following answer for a general reference. A number of other questions on Stack Overflow sound like they promise a quick answer, but are actually asking something more specific.

  • Regex : match only one occurrence of character
  • regex to match a single character that is anything but a space
  • Replace character in regex match only
like image 844
Suragch Avatar asked Nov 29 '17 05:11

Suragch


People also ask

How do I match a specific character in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

Which of the following regular expressions matches any one character?

Use \s to match any single whitespace character.

How do you match a character except one regex?

To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself. The character '. ' (period) is a metacharacter (it sometimes has a special meaning).

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).


1 Answers

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
like image 130
Suragch Avatar answered Oct 07 '22 10:10

Suragch