Python 创建当存在特定类型的非重复字符时匹配的正则表达式
我正在尝试创建一个正则表达式,当没有特定类型的重复字符且忽略所有其他字符时,它将匹配。字长无关紧要。前Python 创建当存在特定类型的非重复字符时匹配的正则表达式,python,regex,Python,Regex,我正在尝试创建一个正则表达式,当没有特定类型的重复字符且忽略所有其他字符时,它将匹配。字长无关紧要。前 hippos-R-Y-S <--- Matches hippos-R-Y-Y <--- Does not match hippos-R-Y-P <--- Matches hippos-R-Y-S这应该可以做到: ^.*?-([YRPS])-(?!\1)([YRPS])-(?!\1)(?!\2)([YRPS])$ 说明: ^ : begining of
hippos-R-Y-S <--- Matches
hippos-R-Y-Y <--- Does not match
hippos-R-Y-P <--- Matches
hippos-R-Y-S这应该可以做到:
^.*?-([YRPS])-(?!\1)([YRPS])-(?!\1)(?!\2)([YRPS])$
说明:
^ : begining of string
.*?- : 0 or more any char until dash
([YRPS]) : one of the set, captured in group 1
- : a dash
(?!\1) : negative lookahead, not the same letter as in group 1
([YRPS]) : one of the set, captured in group 2
- : a dash
(?!\1) : negative lookahead, not the same letter as in group 1
(?!\2) : negative lookahead, not the same letter as in group 2
([YRPS]) : one of the set, captured in group 3
$ : end of string
这应该可以做到:
^.*?-([YRPS])-(?!\1)([YRPS])-(?!\1)(?!\2)([YRPS])$
说明:
^ : begining of string
.*?- : 0 or more any char until dash
([YRPS]) : one of the set, captured in group 1
- : a dash
(?!\1) : negative lookahead, not the same letter as in group 1
([YRPS]) : one of the set, captured in group 2
- : a dash
(?!\1) : negative lookahead, not the same letter as in group 1
(?!\2) : negative lookahead, not the same letter as in group 2
([YRPS]) : one of the set, captured in group 3
$ : end of string
您可以基于2个负lookahead使用此正则表达式跳过[YRPS]
类中匹配的重复字符:
^[^-]+-([YRPS])(?:-(?!\1)([YRPS])(?!.*\2))+$
**正则表达式分解:*
^ # line start
[^-]+ # match 1 or more of any char that is not a -
- # match literal -
([YRPS]) # match [YRPS] and group it #1
(?: # start non-capturing group
- # match literal -
(?!\1) # negative lookahead to assert next char is not same as group #1
([YRPS]) # match [YRPS] and group it #2
(?!.*\2) # negative lookahead to assert next char is not same as group #2
)+ # end non-capture group and + makes it match 1 or more of the same set
$ # end of line
您可以基于2个负lookahead使用此正则表达式跳过[YRPS]
类中匹配的重复字符:
^[^-]+-([YRPS])(?:-(?!\1)([YRPS])(?!.*\2))+$
**正则表达式分解:*
^ # line start
[^-]+ # match 1 or more of any char that is not a -
- # match literal -
([YRPS]) # match [YRPS] and group it #1
(?: # start non-capturing group
- # match literal -
(?!\1) # negative lookahead to assert next char is not same as group #1
([YRPS]) # match [YRPS] and group it #2
(?!.*\2) # negative lookahead to assert next char is not same as group #2
)+ # end non-capture group and + makes it match 1 or more of the same set
$ # end of line
字符串的格式是否始终为^anything-[YRPS]-[YRPS]-[YRPS]$
?字符串的格式是否始终为^anything-[YRPS]-[YRPS]$
?