为了账号安全,请及时绑定邮箱和手机立即绑定

使 python re.sub 多次查看

使 python re.sub 多次查看

FFIVE 2023-06-06 17:18:52
假设我有以下代码:s = 'cucumber apple tomato'def f(match):    if match.group(2) not in ('apple', ):        return '%s (%s)' % (match.group(1), match.group(2))    else:        return match.group()如何进行re.sub(r'([a-z])+\s+[a-z]+', f, s)输出cucumber apple (tomato)?问题是正则表达式引擎只测试cucumber apple,而不是apple tomato。
查看完整描述

2 回答

?
慕的地8271018

TA贡献1796条经验 获得超4个赞

使用捕获前瞻:


>>> s = 'cucumber apple tomato'

>>> re.findall(r'(\w+)(?=[ \t]+(\w+))', s)

[('cucumber', 'apple'), ('apple', 'tomato')]

这使您可以在不消耗字符串的情况下捕获第一个单词前面的第二个单词。


你可以变成(我>>认为<<)是你想要的结果:


>>> [f'{t[0]} ({t[1]})' if t[1]=='apple' else t for t in re.findall(r'(\w+)(?=[ \t]+(\w+))', s)]

['cucumber (apple)', ('apple', 'tomato')]

在您的评论中,您有一个不同的示例和不同的答案模式。对于该结果,只需使用可选匹配项:


>>> s='cucumber apple tomato tomato apple cucumber tomato tomato'

>>> [f'{t[0]} {t[1]} ({t[2]})' if t[2] else f'{t[0]} ({t[1]})' for t in re.findall(r'(\w+)(?:[ \t]+(\w+))?(?:[ \t]+(\w+))?', s)]

['cucumber apple (tomato)', 'tomato apple (cucumber)', 'tomato (tomato)']


查看完整回答
反对 回复 2023-06-06
?
慕神8447489

TA贡献1780条经验 获得超1个赞

这是基于您在评论中提供的信息,因此可能不完全是您要查找的信息,但是:

可以有任意数量的单词:'cucumber apple tomato tomato apple cucumber tomato tomato' 输出应该是 'cucumber apple (tomato) tomato apple (cucumber) tomato (tomato)'

此正则表达式将捕获“apple”之后和行尾之前的所有非空格字符,同时忽略以“apple”结尾的单词并允许它成为行中的第一个。

(?:^| )apple ([^ ]*)|([^ ]+)$

对于示例字符串
“apple cucumber pineapple tomato tomato apple cucumber tomato tomato”,
它将选择
“apple cucumber pineapple tomato tomato apple cucumber tomato tomato ”


查看完整回答
反对 回复 2023-06-06
  • 2 回答
  • 0 关注
  • 93 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信