答案 0 :(得分:2)
答案 1 :(得分:1)
在匹配不应该包含字符的序列时,只需使用字符集即可反转不需要的字符。根据您的示例,我也对此进行了简化。唯一的缺点是比赛将包括尾随空格。
r'.*(#\d*\,?\d+)\s+in\s+([^(>]*)'
>>> for test in tests:
print(re.findall(r'.*(#\d*\,?\d+)\s+in\s+([^(>]*)', test))
[('#26,968', 'Office Products ')]
[('#13,452', 'Industrial & Scientific ')]
[('#99,999', 'baby')]
[('#888', 'office supplies')]
答案 2 :(得分:0)
这可能不是最好的模式,并且可能会流行很多,但是如果提供的样本是对数据的良好采样,那么我可以建议另一种模式:
r"([#\d,]+) in ([\w\s&]+)>?([\w\s&]*)([()\w\s\d]*)"
https://regex101.com/r/hKD6AX/2
希望这会有所帮助!