Reputation: 11
I want to run two regex on a string. If both match, give match.
I use this to check that the string contains /en/
(\b(\w*\/en\/\w*)\b)
'https://google.com/en/article/?mobile=true' gives match 'https://google.com/ru/article/?mobile=true' gives no match
I also use this to check that there is no ? in the string
(^[^\?]*$)
'https://google.com/en/article/' gives match since it doesn't include ? 'https://google.com/en/article/?mobile=true' does not give match
I tried adding them together like this:
(^[^\?]*$)(\b(\w*\/en\/\w*)\b)
However it produces no match in any case. I assume it has to do with pointer position and that the second () needs to specify that checking should start from the beginning?
Upvotes: 1
Views: 220
Reputation: 5059
(?=^[^\?]*$)(?=.*\/en\/.*).*
Both of your regexes can be placed into positive lookaheads to ensure the following string matches them. This can be repeated for any number of conditions you would like to be true.
Upvotes: 1
Reputation: 163447
You could turn the first pattern in a positive lookahead (?=
if that is supported:
^(?=.*\/en\/)[^?]+$
^
Start of string(?=.*\/en\/)
Positive lookahead assert what is on the right contains /en/
[^?]+
Match 1+ times not ?
$
End of string.Upvotes: 1