1

I am trying to match all statements which contain the following two words

ALPHA and BETA

The regex that I used is

(?=.*ALPHA)(?=.*BETA)

I understand this means looking ahead any character for any number occurences check if ALPHA and BETA are present in the given string.

Can someone tell me if there will be any consequence if change the above code to the following.

(?=.*?ALPHA)(?=.*?BETA)

What would this additional question mark do?

My input string would be something like "ALPHA|checkagainst|(BETA_val)". The regex I wrote should return true for this above input as both words ALPHA and BETA are present

Sanju
  • 169
  • 2
  • 8
  • Its just positive lookahead which you are using currently, it just matches it without including that into the match object. – Kunal Mukherjee May 15 '19 at 07:33
  • Give some examples of statements and expected outputs – ALFA May 15 '19 at 07:35
  • Generally the second version ([non greedy / lazy](https://stackoverflow.com/questions/2301285/what-do-lazy-and-greedy-mean-in-the-context-of-regular-expressions)) will perform better, as it looks for as few steps as possible to match. If you expect something to match earlier in string and the other part to match at the end you can also mix [like this demo](https://regex101.com/r/isey8v/1). Be aware that adding a start anchor `^` can make a bigger difference in performance. Else the lookaheads will be triggered at [any position](https://regex101.com/r/isey8v/2). – bobble bubble May 15 '19 at 07:47
  • If this is all pattern / to check for the two substrings, you can further [drop the second lookahead](https://regex101.com/r/isey8v/3) (normal matching for second word). – bobble bubble May 15 '19 at 07:53

2 Answers2

2

It is in relation to greedy and non-greedy quantifiers. The question mark indicates the match is non-greedy and matches as few characters as possible.

Reference: Quantifiers cheat sheet

Brock
  • 31
  • 5
1

Here you go:

([^.]*ALPHA[^.]*)([^.]*BETA[^.]*)

DEMO

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105