There are several built in modules that are part of the Python Standard Library included with your Python installation, such as os (Operating System) and re (Regular Expression) and sys (System).
There are a few Python modules that can be used to determine if a string or integer does or does not contain a matching expression.
- find - returns the first matching patterns anywhere in a string or integer
- re.findall - returns a list containing all of the matching patterns anywhere in a string or integer
- re.match - evaluates to true if there is a single match of a pattern at the beginning of a string or integer
- re.search (this article) - evaluates to true if there is a single match of a pattern anywhere in a string or integer
re.match checks for a match at the very beginning of a string or integer. If the pattern is not found at the beginning of the string or integer, re.match will return None. In this way, re.match is similar to the ^ regular expression, checking for a match at the beginning of the string or integer.
In this example, re.match should evaluate to true because the foo variable begins with "Hello".
#!/usr/bin/python
import re
foo = "Hello World"
if re.match('.*Hello.*', foo, re.IGNORECASE):
print("It's a match. The foo variable begins with Hello")
If you instead want to search for a pattern anywhere in a string or integer, you can use re.search. re.search check for a match anywhere in the string or integer.
#!/usr/bin/python
import re
foo = "Well Hello World"
if re.search('.*Hello.*', foo, re.IGNORECASE):
print("It's a match. The foo variable contains Hello")
Did you find this article helpful?
If so, consider buying me a coffee over at 