Metacharacters in Python
Metacharacters in Python
Metacharacters are a very important concept of the regular expression that helps us to
solve programming tasks using the Python regex module. In this tutorial, we will learn
about the metacharacters in Python or how we can use them. We will explain each
metacharacter along with their short and simple example. The prerequisite of learning
metacharacters is that you should be familiar with the Python regular expression. If not,
visit our Python regular expression tutorial.
We can also specify the range of the characters using the - dash.
Example -
1. import re
2. str1 = "Python is a most popular programming language. Javatpoint is best resou
rce to learn it."
3. res = re.findall(r"[jtp]", str1)
4. print(res)
Output:
['t', 't', 'p', 'p', 'p', 'J', 't', 'p', 't', 't', 't', 't']
Let's see another example - Suppose we want to search the match #a, where a is a
characters followed by the # special character.
Below is the table of some special characters which is used with the \.
Characters Description
1. import re
2. str1 = "Python is a most popular programming language. Javatpoint is best resou
rce to learn it."
3. res = re.findall(r"\.", str1)
4. print(res)
Output:
['.', '.']
As we can that, it returned the list containing the two (.) dot.
Output:
P
Peter likes to
Peter's mobile number is - 4564
Example -
1. import re
2.
3. given_string = "Peter likes to \n roam on the road at night"
4. # dot(.) metacharacter to match any character
5. result_match = re.search(r'^\w{5}', given_string)
6. print(result_match.group())
Output:
Peter
In the above code, we used the \w special sequence which matches any lowercase or
uppercase, numbers, and underscore character. The five inside curly braces specify the
alphanumeric character should be occurring precisely five times.
Example -
1. import re
2.
3. given_string = "Peter likes to \nroam on the road at night \nalso likes to eat ice-
creame"
4. # dot(.) metacharacter to match any character
5. result_match = re.search(r"^\w{5}", given_string, re.M)
6. print(result_match.group())
Example -
1. import re
2.
3. given_string = "Peter likes to \nroam on the road at night \nalso likes to eat ice-
cream"
4. # dot(.) metacharacter to match any character
5. result_match = re.search(r"\w{6}$", given_string, re.M)
6. print(result_match.group())
Output:
cream
Observe that we need to match the two consecutive \d (represent any digit). The thing
should be remembered that at the end of the pattern means zero or more repetitions of
the preceding expression. In this case, we are preceding expression with last \d, not all
two of them. We can set the upper limit as we want. However, the lower limit is zero.
Example -
1. import re
2.
3. given_string = "Numbers are 1234, 8061,14567, 70453"
4. # dot(.) metacharacter to match any character
5. result_match = re.findall(r"\d\d*", given_string)
6. print(result_match)
Output:
1. import re
2.
3. given_string = "Numbers are 5, 34, 1234, 8061,14567, 70453"
4. # dot(.) metacharacter to match any character
5. result_match = re.findall(r"\d\d+", given_string)
6. print(result_match)
Output:
Example -
Metacharacters plays a significant role in solving Python regex real-world problem, and they
come in a wide range. In this tutorial, we have included almost every metacharacters with the
proper explanation and the coding example.