I need logical AND in regex.
something like
jack AND james
agree with following strings
-
‘hi jack here is james‘
-
‘hi james here is jack‘
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can do checks using positive lookaheads. Here is a summary from the indispensable regular-expressions.info:
It then goes on to explain that positive lookaheads are used to assert that what follows matches a certain expression without taking up characters in that matching expression.
So here is an expression using two subsequent postive lookaheads to assert that the phrase matches
jackandjamesin either order:Test it.
The expressions in parentheses starting with
?=are the positive lookaheads. I’ll break down the pattern:^asserts the start of the expression to be matched.(?=.*\bjack\b)is the first positive lookahead saying that what follows must match.*\bjack\b..*means any character zero or more times.\bmeans any word boundary (white space, start of expression, end of expression, etc.).jackis literally those four characters in a row (the same forjamesin the next positive lookahead).$asserts the end of the expression to me matched.So the first lookahead says "what follows (and is not itself a lookahead or lookbehind) must be an expression that starts with zero or more of any characters followed by a word boundary and then
jackand another word boundary," and the second look ahead says "what follows must be an expression that starts with zero or more of any characters followed by a word boundary and thenjamesand another word boundary." After the two lookaheads is.*which simply matches any characters zero or more times and$which matches the end of the expression."start with anything then jack or james then end with anything" satisfies the first lookahead because there are a number of characters then the word
jack, and it satisfies the second lookahead because there are a number of characters (which just so happens to includejack, but that is not necessary to satisfy the second lookahead) then the wordjames. Neither lookahead asserts the end of the expression, so the.*that follows can go beyond what satisfies the lookaheads, such as "then end with anything".I think you get the idea, but just to be absolutely clear, here is with
jackandjamesreversed, i.e. "start with anything then james or jack then end with anything"; it satisfies the first lookahead because there are a number of characters then the wordjames, and it satisfies the second lookahead because there are a number of characters (which just so happens to includejames, but that is not necessary to satisfy the second lookahead) then the wordjack. As before, neither lookahead asserts the end of the expression, so the.*that follows can go beyond what satisfies the lookaheads, such as "then end with anything".This approach has the advantage that you can easily specify multiple conditions.