[Solved] Python Regular Expression from File


This will return the elements you want:

import re

s=""'journey (a,b) from station south chennai to station punjab chandigarh
journey (c,d) from station jammu katra to city punjab chandigarh
journey (e) from station
journey (c,d) from station ANYSTRING jammu katra to ANYSTRING city punjab chandigarh
'''

matches_single = re.findall('journey (\([^,]+,[^,]+\)) from (\S+ \S+\s{0,1}\S*) to (\S+ \S+\s{0,1}\S*)', s)
for match in matches_single:
    print(match)
matches_line = re.findall('(journey \([^,]+,[^,]+\) from \S+ \S+\s{0,1}\S* to \S+ \S+\s{0,1}\S*)', s)
for match in matches_line:
    print(match)

13

solved Python Regular Expression from File