简体   繁体   English

正则表达式与 Python 从行尾匹配

[英]Regex match with Python re from end of line

I'm matching the last word 'City' or 'City City'.我匹配最后一个单词“City”或“City City”。

Work's on regex101.com ( https://regex101.com/r/7F6Jao/1 ) but not in Python.适用于 regex101.com ( https://regex101.com/r/7F6Jao/1 ) 但不在 ZA7F5F35426B927411FC9231B56382.

folder = i.find ( 'folder' ).text
# Top > Continent > Country > City
    country = re.match ( r'\s+\S*$', folder )
    print ( folder )

Output I get 'None'. Output 我得到“无”。

You should be using re.search here, since you don't want your regex pattern to be anchored to the start of the input (which is the default behavior for re.match ):您应该在此处使用re.search ,因为您不希望将正则表达式模式锚定到输入的开头(这是re.match的默认行为):

text = "Top > Continent > Country > City"
p = re.compile("\\b\\S*$")
matches = p.search(text)
if matches:
    print("Found a match: " + matches.group(0))
else:
    print("no match")

This prints:这打印:

Found a match: City

EDIT:编辑:

Thank you for pointing me in the right direction.感谢您为我指明正确的方向。

In addition for the scenario (for City names with 2 words):另外对于场景(对于带有 2 个单词的城市名称):

# Top > Continent > Country > City City 
p = re.compile("[^ ]+\\s+[^ ]+$") 

Output: Output:

City City 
> City 

Seems like cannot exclude the last '>'.似乎不能排除最后一个'>'。

Maybe match from the left capture everything after 3 '>' plus a space?也许从左边匹配捕获 3 '>' 后的所有内容加上一个空格?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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