regex match string python code example
Example 1: python pattern matching example
import re
pattern = re.compile('[a-zA-Z ]+')
message = "Weather is nice"
if pattern.match(message).group() == message:
print("match")
else:
print("no match")
Example 2: python regular expression
import re
test_string = 'Hello greppers!'
pattern = re.compile(r'Hello')
match = pattern.finditer(test_string)
for match in matches:
print(match)
Example 3: re python3
import re
>>> m = re.search('(?<=abc)def', 'abcdef')
>>> m.group(0)
'def'
Example 4: regex in python
import re
sample = "Nothing lasts... but nothing is lost"
found = re.findall("thing", sample)
print(found)
Example 5: python re.search()
match = re.search(r'bb', 'aabbcc')
match = re.search(r'cd', 'aabbcc')
match = re.search(r'...c', 'aabbcc')
match = re.search(r'\d\d\d', 'p123g')
match = re.search(r'\w\w\w', '@@abcd!!')
Example 6: regex in python
>>> import re
>>> p = re.compile('ab*')
>>> p
re.compile('ab*')