Search code examples
regexregex-negationregex-lookaroundsregex-groupregular-language

Not able to not match substring and match string


I am quite new to regular expressions and I have been strugling with finding the correct one for my need an it is the following: I need to get if the string starts with a "n" or a "p" and for that I have ^(n|p). Next up any three of combinations can happen and for that I have (fcombination|scombination|tcombination), after that it's where I problem start to rise. I need to match if "hvt" happens after previous conditions, but not if it's just "hv" other than "hv" any substring should be match.

Can any of the experts help?

Regards


Solution

  • If I understand what you're after, the regex ^[np][fst]combination(?!hv(?!t)).* would match

    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