Reputation: 19
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
Upvotes: 1
Views: 1682
Reputation:
>>> 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']
Upvotes: 1
Reputation: 251031
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'
Upvotes: 2
Reputation: 21783
re.split
by \s+
and take the second last token in the returned list (e.g. using index -2
).
http://docs.python.org/2/library/re.html#re.split
Upvotes: 0