您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

您如何将这种正则表达式习语从Perl转换为Python?

您如何将这种正则表达式习语从Perl转换为Python?

从开始Python 3.8,并引入赋值表达式(PEP 572):=运算符),我们现在可以将条件值捕获到re.search(pattern, text)变量match中,以便检查是否存在条件值None,然后在条件主体内重新使用它:

if match := re.search(r'foo(.+)', text):
  # do something with match.group(1)
elif match := re.search(r'bar(.+)', text):
  # do something with match.group(1)
elif match := re.search(r'baz(.+)', text)
  # do something with match.group(1)
python 2022/1/1 18:45:50 有308人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶