简体   繁体   English

python regex-子字符串匹配的匹配尾

[英]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. 我希望结果有两个组,第一个包含“ Suite 250”,第二个包含其余的字符串。 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.) 匹配任何字符(换行符除外,除非您使用DOTALL标志。)

Two things are wrong with your pattern. 您的模式有两点错误。 1) You are not capturing the "Suite \\d+" part as it is not surrounded by parentheses. 1)您没有捕获“ Suite \\ d +”部分,因为它没有被括号包围。 2) "\\S" matches any character except white space, this is why you only capture the first word. 2)“ \\ S”匹配除空格以外的任何字符,这就是为什么您只捕获第一个单词的原因。

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

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