Python 正则表达式:如何对不立即开始的字符串进行负前瞻? [英] Python regex: how to do a negative lookahead with a string which don't start immediately?

查看:29
本文介绍了Python 正则表达式:如何对不立即开始的字符串进行负前瞻?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 https://docs.python.org/2/library/re.html#regular-expression-syntax :

(?!...)
如果 ... 与 next 不匹配,则匹配.这是一个否定的前瞻断言.例如,Isaac (?!Asimov) 将匹配 'Isaac ' 仅当它后面没有 'Asimov' 时.

(?!...)
Matches if ... doesn’t match next. This is a negative lookahead assertion. For example, Isaac (?!Asimov) will match 'Isaac ' only if it’s not followed by 'Asimov'.

但是如果 IsaacAsimov 之间有一个或几个词怎么办?

But what to do if there is one or several words between Isaac and Asimov?

推荐答案

Isaac(?!.*Asimov)

对任何以阿西莫夫结尾的字符序列使用负前瞻.[\s\S]DOTALL 标志可能适合在 . 中包含换行符.

Use a negative lookahead for any sequence of characters ending in Asimov. [\s\S] or the DOTALL flag may be appropriate if you want to include newlines in the ..

这篇关于Python 正则表达式:如何对不立即开始的字符串进行负前瞻?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆