正则表达式子字符串在字符串的任何位置不匹配 [英] Regex substring one mismatch in any location of string

查看:68
本文介绍了正则表达式子字符串在字符串的任何位置不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释为什么下面的代码返回一个空列表:

<预><代码>>>>进口重新>>>m = re.findall("(SS){e<=1}", "PSSZ")>>>米[]

我正在尝试在 PSSZ 中找出 SS(并包含最多一个不匹配的可能性)的总出现次数.

我在这里看到了一个类似的代码示例:搜索允许在字符串的任何位置出现一个不匹配的字符串

解决方案

您需要删除范围量词中存在的 e<= 字符.范围量词必须是 ,

  • {n} .重复前一个标记 n 次.
  • {min,max} 从 min 到 max 重复前一个标记.

应该是,

m = re.findall("(SS){1}", "PSSZ")

m = re.findall(r'SS','PSSZ')

更新:

<预><代码>>>>re.findall(r'(?=(S.|.S))', 'PSSZ')['PS', 'SS', 'SZ']

Can someone explain why the code below returns an empty list:

>>> import re
>>> m = re.findall("(SS){e<=1}", "PSSZ")
>>> m
[]

I am trying to find the total number of occurrences of SS (and incorporating the possibility of up to one mismatch) within PSSZ.

I saw a similar example of code here: Search for string allowing for one mismatch in any location of the string

解决方案

You need to remove e<= chars present inside the range quantifier. Range quantifier must be of ,

  • {n} . Repeats the previous token n number of times.
  • {min,max} Repeats the previous token from min to max times.

It would be,

m = re.findall("(SS){1}", "PSSZ")

or

m = re.findall(r'SS','PSSZ')

Update:

>>> re.findall(r'(?=(S.|.S))', 'PSSZ')
['PS', 'SS', 'SZ']

这篇关于正则表达式子字符串在字符串的任何位置不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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