社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Cary Swoveland  »  全部回复
回复总数  1
4 年前
回复了 Cary Swoveland 创建的主题 » Python正则表达式匹配字母表中的3个连续字符,但不一定并排

你问是不是 可能的 要用正则表达式实现这一点?当然它漂亮吗?这在旁观者的眼里。

您需要一个如下所示的正则表达式(设置大小写无关标志):

^(?=.*\d)(?=.*[a-z])(?=.*[<special symbols here>])(?!<no 3 digits that are consecutive>)(?!<no three letters that are consecutive>).{8}

让我们看看 消极前瞻

(?!<no 3 digits that are consecutive>) 

我们可以这样写。

(?!(?:(?=.*0)(?=.*1)(?=.*2))|(?:(?=.*1)(?=.*2)(?=.*3))|(?:(?=.*2)(?=.*3)(?=.*4))|(?:(?=.*3)(?=.*4)(?=.*5))|(?:(?=.*4)(?=.*5)(?=.*6))|(?:(?=.*5)(?=.*6)(?=.*7))|(?:(?=.*6)(?=.*7)(?=.*8))|(?:(?=.*7)(?=.*9)(?=.*9)))

Demo

这个表达式可以用英文写成 冗长的 模式( re.X 或 re.VERBOSE )让它自我记录。

(?!          # begin negative lookahead       
  (?:        # begin non-capture group
    (?=.*0)  # match > 0 characters followed by 0 (positive lookahead) 
    (?=.*1)  # match > 0 characters followed by 1 
    (?=.*2)  # match > 0 characters followed by 2
  )          # end non-capture group
  |          # or
  ... similar for (?:(?=.*1)(?=.*2)(?=.*3))
  ...
)            # end negative lookahead

建筑

(?!<no three letters that are consecutive>)

相似(包含23个元素的交替, a , b 和 c 到 x , y 和 z ).