首页 > 解决方案 > 无法不匹配子字符串和匹配字符串

问题描述

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

有哪位高手可以帮忙吗?

问候

标签: regexregex-negationregex-lookaroundsregex-groupregular-language

解决方案


如果我了解您的需求,则正则表达式^[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

推荐阅读