python - Return Match with respect to Occurrence ,Regex -


i have many strings (in series format) , list of words in csv format. need match expression , return word @ top in csv . example : have them

hsr layout   aecs layout   garden layout   k aecs layout   

and suppose string contains :

str1 = "room no 135 chancery hotel,block k aecs layout"   

since aecs layout occurs above k aecs layout , want expression match aecs layout . code returns latter. how prioritize it?

my code:

str1 = "room no 135 chancery hotel,block k aecs layout"  layouts_string1 =r'({})'.format('|'.join(['hsr layout','aecs layout','garden layout','k aecs layout'])) layout1_re = re.compile(layouts_string1) ms = layout1_re.search(str1) print ms.group() 

but returns "k aecs layout" . how 1 i.e aecs layout comes first in '|' expression?

the reason k aecs layout matches, rather aecs layout, because k letter comes before a letter, , regex finds match on k rather later a.

one workaround write loop:

str1 = "room no 135 chancery hotel,block k aecs layout" match in ['hsr layout','aecs layout','garden layout','k aecs layout']:     layout1_re = re.compile(match)     ms = layout1_re.search(str1)     if ms:         print ms.group()         break  out: >>>  aecs layout 

the reason have loop through multiple times because: if want prioritise matches, have check end of string highest priority match before can check string other matches.

your use case quite simple, though, , if may suggest simpler solution without regex:

str1 = "room no 135 chancery hotel,block k aecs layout" match in ['hsr layout','aecs layout','garden layout','k aecs layout']:     if match in str1:         print match         break 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -