
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If a String Matches Regex List in Python
A regular expression is also referred as regex, which is a sequence of characters that forms a search pattern. It's one of the powerful tools used for pattern matching and manipulating strings. In python we are having a module called re which helps to form a regular expression.
Regex patterns consist of ordinary characters such as letters, digits and special characters called metacharacters. Meta Characters have special meanings and allow us to define complex search patterns. The below are some commonly used meta characters in python regular expressions.
. (dot) ? Matches any single character except a newline.
^ (caret) ? Matches the start of a string.
$ (dollar) ? Matches the end of a string.
* (asterisk) ? Matches zero or more occurrences of the preceding pattern.
+ (plus) ? Matches one or more occurrences of the preceding pattern.
? (question mark) ? Matches zero or one occurrence of the preceding pattern.
[] (square brackets) ? Defines a character class and matches any single character within the brackets.
() (parentheses) ? Groups patterns together and captures matched substrings.
\ (backslash) ? Escapes a metacharacter or introduces a special sequence.
The other Additional regular expressions support special sequences that represent common patterns are mentioned as below.
\d ? Matches any digit equivalent to [0-9].
\w ? Matches any alphanumeric character equivalent to [a-zA-Z0-9_].
\s ? Matches any whitespace character.
\b ? Matches a word boundary.
\A ? Matches the start of a string similar to ^, but it doesn't respect multiline mode.
\Z ? Matches the end of a string similar to $, but it doesn't respect multiline mode.
There are multiple approaches in python to check if a string matches a regular expression (regex) list. Let's see each approach one by one.
Using re module
The 're' module in Python provides functions to work with regular expressions. We can use the 're.match()' function in re module to check if a string matches a regex pattern and to check a list of regex patterns, we can iterate over the list and call 're.match()' for each pattern.
Example
In this example, the re.match() function is used to check if the string matches each pattern in the regex_list. If match is found, it will print the pattern.
import re string = "Hello, Welcome to Tutorialspoint!" regex_list = [r"Hello", r"\bWelcome\b", r"\d+"] for pattern in regex_list: if re.match(pattern, string): print(f"String matches pattern: {pattern}")
Output
String matches pattern: Hello
Using list comprehension and re.search()
The other way of finding the matched pattern is to use list comprehension and the re.search() function. By using the list comprehension without iterating over the regex list, we can create a new list containing the matching patterns.
Example
In this example, we are using the list comprehension with the line of code [pattern for pattern in regex_list if re.search(pattern, string)] which creates a new list matching_patterns that contains the regex patterns from regex_list that match the string. Then we use the re.search() function to find the first occurrence of a pattern in the string.
import re string = "Hello,happy learning!" regex_list = [r"Hello", r"\bWelcome\b", r"\d+"] matching_patterns = [pattern for pattern in regex_list if re.search(pattern, string)] print("Matching patterns:", matching_patterns)
Output
Matching patterns: ['Hello']
Using the any() function and re.search()
The any() function is one of the functions available in python which can be used to check if any element in a sequence of the regex list is true. We can combine this with re.search() to check if any regex pattern matches the string.
Example
In this example, the any() function is used to iterate the elements of the regex list and check if any pattern matches the string using re.search(). If match is found, it will print "String matches at least one pattern".
import re string = "Hello, Welcome to Tutorialspoint!" regex_list = [r"Hello", r"\bWelcome\b", r"\d+"] if any(re.search(pattern, string) for pattern in regex_list): print("String matches at least one pattern")
Output
String matches at least one pattern