Regular expression special Symbols (Metacharacters)
Regular expression special Symbols (Metacharacters): -
- Special characters are characters with a special meaning in regular expressions.
1) [ ] (square
brackets): -
- It represents a set of characters.
- We can also specify a range of characters using – (hyphen) inside the square brackets.
- Example:
[abc] will match "a",
"b", or "c".
[0,3] is same as [0
1 2 3]
[a-c] is same as [a
b c]
- We can also invert the character class using ^ (caret) symbol.
[^0-3] means any
number except 0, 1 , 2 , or 3.
[^a-c] means any
character except a , b , or c.
Example: -
output: -
2) \ (Backslash): -
- In Python, the backslash (\) is called an escape character.
- It is used to insert special characters into a string that cannot be typed directly.
- Example: "\d+" matches "123", "45".
- \w → Matches any word (alphanumeric) character ((a-z, A-Z,) - letters, (0-9) digits, ( _ ) underscore).
- \s → Matches any whitespace character (space, tab, newline).
Example: -
output: -
3) . (dot): -
- It matches only any single character except for newline character (\n).
- Example: "c.t" matches "cat", "cut", "cot".
Example: -
output: -
4) ^ (caret): -
- It matches the beginning of a string and checks whether the string ends with given character or characters or not.
- Example: ^Hello matches "Hello World" but not "Say Hello".
Example: -
output: -
5) $ (dollar): -
- It matches the end of a string and checks whether the string starts with given character or characters or not.
- Example: world$ matches "Hello world".
Example: -
output: -
6) *(asterisk): -
- It matches zero or more occurrences with * symbol.
- ab*c will be matched for the string ac, abc, dabc etc. but will not be matched for abdc because b is not followed by c.
- Example: "go*" matches "g", "goo", "google".
Example: -
output: -
7) + (plus): -
- It matches one or more occurrences with + symbol.
- ab+c will be matched for the string abc , abbc , dabc, but will not matchwd for ac , abdc because there is no b in ac and b is not followed by c in abdc.
- Example: "go+" matches "go", "goo", but not "g".
Example: -
output: -
8) ? (question mark):
-
- It matches zero or one occurrence with ? symbol.
- a?b → Matches "b" or "ab", but not "aab".
- Example: "colou?r" matches "color" and "colour".
Example: -
output: -
-
Hyphen within a character set indicates a range. [a-z] matches any lowercase
letter.
|
Pipe acts as an "OR" operator. (cat|dog) matches either
"cat" or "dog".