Character classes are used to match the string of characters. These classes let the user match any range of characters, which user don’t know in advance. Set of characters that to be matched is always written between the square bracket . A character class will always match exactly for one character. If match not found then the whole regex matching fails.
Suppose you have a lot of strings like #g#, #e#, #k#, #k#, #s#, #.# or #@# and you have to match a # character which is followed by ‘g‘, ‘e‘, ‘k‘, ‘s‘, ‘.‘, or ‘@‘, followed by the another # character, then try the regex /[#geeks@.#]/ that will match the required. It will start a match with # and then match any character in  and after that match another #. This regex will not match “##” or “#ge#” or “#gg#” etc. because as said earlier that the character class always match exactly one character between the two ‘#‘ characters.
- The Dot(.) inside the character class, lost its special meaning i.e. “everything except newline“.
- The Dot(.) can match a single dot(.) only inside a character class.
- Most of the special characters lose their special meaning inside a character class, but there some characters that get some special meaning inside a character class.
Range In Character Class: To match a long list of characters is very difficult to type because it may be a possibility that user might skip one or two characters. So to make the task easy we will use range. Generally, a dash(-) is used to specify the range.
To specify range [abcdef] you can use /[a-f]/
- Range is specified using -(Dash) symbol.
- User can also combine multiple range of characters, digits etc. like [0-9a-gA-g]. Here ‘–‘ allows user to take any number of character or digit specified in the range
- If the user want to match dash(-) in a given string then he can simply put it between the square brackets .
- To match a closing square bracket in a string, just precede it with \ i.e. \] and put it between the square brackets .
Negated Character Class: To negate a character class just use caret(^) symbol. It will negate the specified character after the symbol or even a range. If you will put a Caret (^) as the first character in the character class it means that character class can match any one character except those mentioned in the character class.
Match Not Found
- Perl | Special Character Classes in Regular Expressions
- Perl | Anchors in Regex
- Perl | Assertions in Regex
- Perl | 'ee' Modifier in Regex
- Perl | Grouping and Alternation in Regex
- Perl | Regex Cheat Sheet
- Perl | Searching in a File using regex
- Perl | Extract IP Address from a String using Regex
- Perl | Classes in OOP
- Perl Tutorial - Learn Perl With Examples
- Perl | Basic Syntax of a Perl Program
- Perl vs C/C++
- Perl | Operators | Set - 2
- Perl vs Java
- Formats in Perl
- Perl | cos() Function
- Perl | oct() Function
- while loop in Perl
- Perl | log() Function
- Perl | qw Operator
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh