繁体   English   中英

python正则表达式分组

[英]python regex grouping

给定字符串s = '(A /something_1)(B /something_2)(C /something_3),/,(D /something_4)(D /something_5)'

我想得到以下输出: (C /something_3),/,(D /something_4)(D /something_5)

我一直在匹配整个字符串s,而不是在子字符串之上。

我正在使用re.search(r'(\\(C.*\\)),/,(\\(D.*\\))+')

任何帮助表示赞赏...

您就在那里re.search(r'(\\(C.*\\)),/,(\\(D.*\\))+', s).group()将为您提供所需的东西。

>>> import re
>>> s = '(A /something_1)(B /something_2)(C /something_3),/,(D /something_4)(D /something_5)'
>>> re.search(r'(\(C.*\)),/,(\(D.*\))+', s).group()
'(C /something_3),/,(D /something_4)(D /something_5)'

您是否希望将其进一步分组?

使用Python 2.7,我得到的确切结果是:

import re
s = '(A /something_1)(B /something_2)(C /something_3),/,(D /something_4)(D /something_5)'
m = re.search(r'(\(C.*\)),/,(\(D.*\))+', s)

s[m.start():m.end()] == '(C /something_3),/,(D /something_4)(D /something_5)'

暂无
暂无

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

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