python - Modifying a Regex Expression -
i have several strings match expressions want match , extract 2 words after prepositions along preps themselves.. , job . need modify regex such in case word "to" appears after preposition , regex extract preposition followed 3 words(instead of default 2 words..)
here's example elaborate:
str1 = " place near oberoi mall" str2 = " place next oberoi mall"
expected results:-
res1 = "near oberoi mall"
#extract 2 words after prep along (default case)
res2 = "next oberoi mall"
#extract prep along 3 words after (in case "to" comes after preposition)
what have done?
def landmark(str): preps = ['near','off','next','across','opposite','behind','above','ahead'] words = "|".join(re.escape(line.rstrip()) line in preps) p1 = re.compile(r'(?:{})\s(\w+|\d+\w+)\s\w+'.format(words)) q =re.search(p1,str) if q none: return "" else: return q.group()
my preps in list called preps
in returning 2 words
res1 = "near oberoi mall"
res2 = "next oberoi"
#this becomes incomplete
what did try?
here:
p1 = re.compile(r'(?:{}(?:to)?)\s(\w+|\d+\w+)\s\w+'.format(words))
*notice optional (?:to)? added it. there's small problem.. please help.
this worked example:
>>> p1 = re.compile(r'(?:%s)\s((?:to\s)?(\w+|\d+\w+)\s\w+)' % words) >>> dd = re.search(p1,str1) >>> dd.group() 'near oberoi mall' >>> cc = re.search(p1,str2) >>> cc.group() 'next oberoi mall'
Comments
Post a Comment