无法匹配子字符串和匹配字符串

时间:2019-03-18 19:06:30

标签: regex regex-negation regex-lookarounds regex-group regular-language

我对正则表达式非常陌生,我一直在努力寻找适合我需要的正则表达式,它是: 我需要获取字符串是否以“ n”或“ p”开头,为此我有^(n | p)。 接下来,任何三种组合都可能发生,为此,我遇到了(组合),此后我开始出现问题。 如果“ hvt”在前面的条件之后发生,我需要匹配,但是如果不是“ hv”而只是“ hv”,则任何子串都应该匹配。

任何专家都可以帮忙吗?

致谢

1 个答案:

答案 0 :(得分:0)

如果我了解您的需求,则正则表达式^[np][fst]combination(?!hv(?!t)).*会匹配

nfcombinationhvt  //match
pscombinationhvt  //match
ntcombinationhvt  //match
nfcombinationdrums  //match
pscombinationguitar  //match
ntcombinationkicker  //match
nfcombinationhvxxx  //no match
pscombinationhvzz  //no match
ntcombinationhva  //no match
nfcombinationhv  //no match
pscombinationhv  //no match
ntcombinationhv  //no match
相关问题