Regular Expressions: Is there an AND operator?

后端 未结 12 2009
刺人心
刺人心 2020-11-21 06:34

Obviously, you can use the | (pipe?) to represent OR, but is there a way to represent AND as well?

Specifically, I\'d like to

12条回答
  •  情书的邮戳
    2020-11-21 06:57

    You can do that with a regular expression but probably you'll want to some else. For example use several regexp and combine them in a if clause.

    You can enumerate all possible permutations with a standard regexp, like this (matches a, b and c in any order):

    (abc)|(bca)|(acb)|(bac)|(cab)|(cba)
    

    However, this makes a very long and probably inefficient regexp, if you have more than couple terms.

    If you are using some extended regexp version, like Perl's or Java's, they have better ways to do this. Other answers have suggested using positive lookahead operation.

提交回复
热议问题