Python regex 如何使用 NOT 条件检查组反向引用或忽略反向引用中的组项
Python regex How to check with NOT condition for group backreference OR ignoring a group item in backreference
问题:- 如果备用号码不同,则号码有效。如果备用号码相同且中间号码也相同,则它们再次有效
example:-
123456 :- This number is valid as we don't have any alternate number as same, all are different
110100:- This number is invalid as in 010 alternate numbers are same and in-between is different
110000 :- this number is valid as in 000 alternate number though are same in-between is also same
我试过的
import re
st="1101010"
#st="110000"
re.findall(r"(\d)[^]&[\d]", st)
我试图通过反向引用和使用 AND 条件来取消组项目,但这不起作用。
您可以使用正则表达式
r"(\d)(?!)\d"
匹配此正则表达式的字符串包含字符串 aba
,其中 a
是任何字符,b
是 a
以外的任何字符。
Python 的正则表达式引擎执行以下操作。
(\d) # match a digit and save to cap grp 1
(?!) # the next char cannot be the content of cap grp 1
\d # match a digit
# match the content of cap grp 1
问题:- 如果备用号码不同,则号码有效。如果备用号码相同且中间号码也相同,则它们再次有效
example:-
123456 :- This number is valid as we don't have any alternate number as same, all are different
110100:- This number is invalid as in 010 alternate numbers are same and in-between is different
110000 :- this number is valid as in 000 alternate number though are same in-between is also same
我试过的
import re
st="1101010"
#st="110000"
re.findall(r"(\d)[^]&[\d]", st)
我试图通过反向引用和使用 AND 条件来取消组项目,但这不起作用。
您可以使用正则表达式
r"(\d)(?!)\d"
匹配此正则表达式的字符串包含字符串 aba
,其中 a
是任何字符,b
是 a
以外的任何字符。
Python 的正则表达式引擎执行以下操作。
(\d) # match a digit and save to cap grp 1
(?!) # the next char cannot be the content of cap grp 1
\d # match a digit
# match the content of cap grp 1