Special Characters in Regex


Special Characters in Regex

🙋 Need help? Ask an expert now!

Regular expressions have a set of special characters that each have a different behavior when used. There are special characters that are sued to match multiple characters, match whitespace, match digits, match letters, etc. A list of some of the more frequently used special characters are shown below:

  • * – The preceding character/group is matched 0 or more times
  • + – Almost the same behavior as * - the preceding character/group is matched 1 or more times
  • ? – The preceding character/group is matched 0 or 1 time(s)
  • . – matches any single character except the newline character
  • | – Matches one pattern or the other
  • [xyz] – Character set - Matches any of the characters
  • [x-z] – Character set - Matches any characters between the characters range
  • [^xyz] – Inverted characters set - Matches all other characters
  • {N} – matches exactly N occurrences of the preceding character/group
  • {N, M} – matches at least N and at most M occurrences of the preceding character/group
  • ^ - matches the start of the string
  • $ - matches the end of the string
  • \s – matches a single white space character, including space, tab, form feed, line feed
  • \S – matches a single character other than white space
  • \d – matches any digit
  • \D – matches any non-digit character
  • \w – matches any alphanumeric character including the underscore
  • \W – matches any non-alphanumeric or underscore character
Edit Me on GitHub!

You've Reached the End of this Course! 🎓🎉

Great work, are you ready to learn more?

View More Courses