Multiple Regex Matching Over (Lines) *
A quick and concise way to match regex using two dictionaries to store both the expression itself and the result of the search. Both the key portions of the key:value pair contain the same tags whereas the value contains the regex and the result respectively.
regex = {'title' : "title_regex'",
'auth' : "auth_regex'",
'vers' : "vers_regex'",
'copy' : "copy_regex'"}
result = {'title': '',
'auth' : '',
'vers' : '',
'copy' : ''}
An example regex might be;
"__[tT]itle__[' ']*=[' ']*\'(\w+([' ']*\w*)*)\'"
Test each line in the parsed text against each regex and store the search results. When searching could return multiple matches the below example could be reworked to accommodate a result array as the returned value to the dictionary.
for line in infile:
for expr in regex:
found = re.match(regex[expr], line)
if found:
result[expr] = found.group(1)
Written by Daniel Chauvin
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Related Tags
#python
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#