
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
What are metacharacters inside character classes used in Python regular expression?
Most letters and characters simply match themselves. However, there are some characters called metacharacters, that don’t match themselves. Instead, they indicate that some pattern should be matched, or they repeat or change parts of the regular expression.
Here’s a complete list of the metacharacters
. ^ $ * + ? { } [ ] \ | ( )
At first we’ll look at [ and ]. They’re used for indicating a character class, which is a set of characters that you want to match. Characters can be listed individually, or a range of characters can be indicated by giving two characters and separating them by a '-'. For example, [xyz] will match any of the characters x, y, or z; this is the same as [x-z], which uses a range to express the same set of characters. If you wanted to match only lowercase letters, your regex would be [a-z].
Metacharacters don't work inside classes. For example, [abc$] will match any of the characters 'a', 'b', 'c', or '$'; '$' is a metacharacter, but inside a character class it is stripped of its special nature.
If there is a '^' as the first character of a class it means all those characters that are NOT of this class; For example, [^8] will match any character except '8'.
Perhaps the most significant metacharacter is the backslash, \. It’s also used to escape all the metacharacters so you can still match them in patterns; for example, if you need to match a ] or \, you can precede them with a backslash to remove their special meaning: \] or \.
- Related Articles
- What are repeating character classes used in Python regular expression?
- What are character classes or character sets used in Python regular expression?
- What are negated character classes that are used in Python regular expressions?
- Explain character classes in Java regular expressions
- What are regular expression repetition cases in Python?
- What Python regular expression can be used in place of string.replace?
- How to escape any special character in Python regular expression?
- How to match a nonwhitespace character in python using Regular Expression?
- How to match a single character in python using Regular Expression?
- What is a regular expression in Python?
- What does “?:” mean in a Python regular expression?
- Regular Expression Modifiers in Python
- Regular Expression Patterns in Python
- Regular Expression Examples in Python
- Regular Expression Matching in Python
