[英]Python Regular Expression to find the last occurrence of whitespace in a certain pattern
I am using Regular expression in Python. 我在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 ]".
“星期一,[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
). re.split
\\s+
re.split
by \\s+
并获取返回列表中的第二个最后一个标记(例如,使用索引-2
)。
http://docs.python.org/2/library/re.html#re.split 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']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.