I'm matching the last word 'City' or 'City City'.
Work's on regex101.com ( https://regex101.com/r/7F6Jao/1 ) but not in Python.
folder = i.find ( 'folder' ).text
# Top > Continent > Country > City
country = re.match ( r'\s+\S*$', folder )
print ( folder )
Output I get 'None'.
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
):
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):
# Top > Continent > Country > City City
p = re.compile("[^ ]+\\s+[^ ]+$")
Output:
City City
> City
Seems like cannot exclude the last '>'.
Maybe match from the left capture everything after 3 '>' plus a space?
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.