简体   繁体   中英

Python Regular Expression to find the last occurrence of whitespace in a certain pattern

I am using Regular expression in Python. I want to find the string before last occurrence of whitespace in a certain pattern. For example In the following text, I want to find "Street". "On Monday , a worker at a [LOC Te Rapa Tika Street ]".

Can anyone help me to find the string using regular expression?

Thanks

Split the string on spaces and get the second last element:

>>> strs = "On Monday , a worker at a [LOC Te Rapa Tika Street ]"
>>> strs.split()[-2]
'Street'

re.split by \\s+ and take the second last token in the returned list (eg using index -2 ).

http://docs.python.org/2/library/re.html#re.split

>>>  import re
>>>  match = re.search('\[\s?LOC.+\s(\w+)\s?\]', "[LOC Te Rapa Tika Street ]")
>>>  match.group(1)
'Street'

This should work regardless of the spacing on the brackets.

Edit: After reading your comment, this would work better

   >>>  import re
   >>>  sentence = "A man strolling through the [LOC Pullman Hotel ] in [LOC Waterloo Quadrant ] on Sunday with the bag across his shoulder"
   >>>  match = re.findall('\[\s?LOC[^\]]+\s(\w+)\s?\]', sentence)
   >>>  match
   ['Hotel', 'Quadrant']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM