Matching two words with some characters in between in regular expression

前端 未结 4 496
孤独总比滥情好
孤独总比滥情好 2021-01-07 14:55

I want to do a match for a string when no abc is followed by some characters (possibly none) and ends with .com.

I tried with the following

4条回答
  •  暖寄归人
    2021-01-07 15:55

    Do you just want to exclude strings that start with abc? That is, would xyzabc.com be okay? If so, this regex should work:

    ^(?!abc).+\.com$
    

    If you want to make sure abc doesn't appear anywhere, use this:

    ^(?:(?!abc).)+\.com$
    

    In the first regex, the lookahead is applied only once, at the beginning of the string. In the second regex the lookahead is applied each time the . is about to match a character, ensuring that the character is not the beginning of an abc sequence.

提交回复
热议问题