简体   繁体   English

正则表达式以匹配括号内的两组字符串

[英]Regex to match two groups of strings within brackets

I have a block of text that includes the following: 我有一段包含以下内容的文字:

race={ heat1={ names={ ALEX DAN PETER } } heat2={ names={ RYAN DYLAN } } }

How would I build a regex to get all of the heat1 names into group 1, and all of the heat2 names into group 2? 我将如何构建一个正则表达式以将所有heat1名称放入组1,并将所有heat2名称放入组2? I can't find a one-for-one question that's already been asked, and the closest I'm able to get is: 我找不到已经提出的一对一问题,而我能得到的最接近的问题是:

(?:names={)( [A-z]+ )

Which returns ALEX and RYAN in the same group. 它在同一组中返回ALEX和RYAN。

With re.findall() function: 使用re.findall()函数:

import re

s = 'race={ heat1={ names={ ALEX DAN PETER } } heat2={ names={ RYAN DYLAN } } }'
result = re.findall(r'heat[12]=\{ names=\{ ([^}]+[^}\s])', s)

print(result)

The output: 输出:

['ALEX DAN PETER', 'RYAN DYLAN']

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

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