Help with regex include and exclude

前端 未结 3 1416
生来不讨喜
生来不讨喜 2020-12-31 09:04

I would like some help with regex.

I\'m trying to create an expression that will include certain strings and exclude certain strings.

For example:

I

3条回答
  •  失恋的感觉
    2020-12-31 09:33

    To match a string which must have word from a set of words you can use positive lookahead as:

    ^(?=.*(?:inc1|inc2|...))
    

    To not match a string which has a word from a list of stop words you can use negative lookahead as:

    ^(?!.*(?:ex1|ex2|...))
    

    You can combine the above two requirements in single regex as:

    ^(?=.*(?:inc1|inc2|...))(?!.*(?:ex1|ex2|...))REGEX_TO_MATCH_URL$
    

    Rubular link

提交回复
热议问题