简体   繁体   中英

python regex - matching tail of substring match

I've got an address string of type

'Suite 100 <building name>, <street number, name + rest of address>'

and I'm trying to extract the suite part and the rest of the address line after the suite part, using regex, but it's not working as expected. Here's what I'm using:

>> res = re.match(r'Suite \d+ (\S+)?', 'Suite 250 Victory Plaza, 100 Sunshine Street, Paradise City 99999')
>> res.groups()
>> ('Victory',)

I want the result to have two groups, the first containing 'Suite 250' and the second to have the rest of the string. How can I do this?

Try the following:

r"(Suite \d+)\s*(.+)"

The parts in parentheses are the groups to be captured. '.' matches any character (except new lines, unless you use the DOTALL flag.)

Two things are wrong with your pattern. 1) You are not capturing the "Suite \\d+" part as it is not surrounded by parentheses. 2) "\\S" matches any character except white space, this is why you only capture the first word.

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.

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