user1522348
user1522348

Reputation: 19

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

Upvotes: 1

Views: 1682

Answers (3)

user425495
user425495

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

Ashwini Chaudhary
Ashwini Chaudhary

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

Patashu
Patashu

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

Related Questions