Saturday, 26 November 2016

Regex concat with "AND" opeartor




I want to concatenate two Regular expressions with AND operator (I don't know if this is possible ... ) so the match happens only when the string matches RegEx1 and RegEx2.



 RegEx1: [a-g]+
RegEx2: [b-z]+
Example1 : String "bcd" match
Example2 : String "hijk" not match


It was easy with OR operator, but for AND I could not find a solution.



Answer



Use a positive lookahead combined with 'start and end of string' anchors to ensure entire string is matched and no "illegal" letters are present:



^(?=[a-g]+$)(?=[b-z]+$).*


https://regex101.com/r/Bz7qnb/2/


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...