regex search python code example
Example 1: Using the Python string below to perform a search using a regular expression that you create. search_string=’’’This is a string to search for a regular expression like regular expression or regular-expression or regular:expression or regular&expression’’’
match1 = re.findall(pattern, search_string)
if match1 != None:
print(pattern + 'matched')
else:
print(pattern + 'did not match')
Example 2: python re compile
import re
prog = re.compile(pattern)
result = prog.match(string)
result = re.match(pattern, string)
Example 3: 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 4: python .findall
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 5: Python Regex documentation\
>>> import re
>>> m = re.search('(?<=abc)def', 'abcdef')
>>> m.group(0)
'def'
Example 6: re python3
import re
>>> m = re.search('(?<=abc)def', 'abcdef')
>>> m.group(0)
'def'