Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Regex - Get Started
  • Regex Syntax
  • Characters
  • Metacharacters
  • Quantifiers
  • Character Classes
  • Grouping
  • Lookarounds
  • Substitution
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Regex Character Classes

In regular expressions, a character class is one or more characters enclosed in square brackets []. Character classes match any one character from the set of characters specified within the brackets.

Character classes make it easy to match a specific set of characters or exclude certain characters from a match. For example, the regular expression [aeiou] matches any vowel, while [^aeiou] matches any character that is not a vowel.

You can also use character classes to match sets of characters, such as [a-z] to match any lowercase letter or [0-9] to match any digit.

Character classes can be combined with other regular expression metacharacters, such as quantifiers and anchors, to create more complex patterns.

Note: Character classes only match a single character at a time, so if you want to match multiple characters in a row, you'll need to use a quantifier like + or *.

The followings are common examples of character classes:

  • [abc] - matches any one character that is either 'a', 'b', or 'c'.
  • [a-z] - matches any one lowercase letter from 'a' to 'z'.
  • [A-Z] - matches any one upper case letter from 'A' to 'Z'.
  • [0-9] - matches any one digit from '0' to '9'. Optionaly, use \d metacharacter.
  • [^abc] - matches any one character that is not 'a', 'b', or 'c'.
  • [\w] - matches any one-word character, including letters, digits, and underscore.
  • [\s] - matches any whitespace character, including space, tab, and newline.
  • [^a-z] - matches any one character that is not a lowercase letter from 'a' to 'z'.

The following demonstrates character classes:

//g
0

The regex pattern [eor] finds all the occurrences of ‘e’, ‘o’, and ‘r’ in the text.

//g
0

The regex pattern [^eor] finds all the occurrences of characters except ‘e’, ‘o’, and ‘r’ in the text.

//g
0

The regex pattern [m-r] finds all the occurrences of characters that come between small m and r, that is m,n,o,p,q,r in the text.

//g
0

The regex pattern [\w] finds all the occurrences of letters, digits, and underscore.

//g
0

The regex pattern [\s] finds all the occurrences of white space, tab, and newline characters.

//g
0

The regex pattern [^a-z] finds all the occurrences of a character that is not in a lowercase letter.

Note that we can combine one or more quantifier characters with other elements in the regex pattern.

Overall, character classes are a powerful tool in regular expressions that allow for precise and flexible pattern matching.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.