Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match first groups occurrence with regex? [duplicate]

Tags:

java

regex

Example: https://regex101.com/r/n4x91E/1

INPUT STRING :

"I think we have to point certain things out to this man, and after that point some other things out to him as well"

MY REGULAR EXPRESSION :

(point).*(out)

RETURNING WRONG RESULT :

"I think we have to point certain things out to this man, and after that point some other things out to him as well"

EXPECTING RESULT :

"I think we have to point certain things out to this man, and after that point some other things out to him as well"

How to change my regular expression to get the first groups occurrence ?

like image 308
阿尔曼 Avatar asked Dec 15 '22 00:12

阿尔曼


2 Answers

You can use a lazy quantifier for your regular expression like in this version:

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

(point).*?(out)

In the Java documentation this quantifier is called reluctant, but I think lazy is more commonly used...

like image 193
dpr Avatar answered Mar 03 '23 02:03

dpr


You can try this:

(point).*?(out)

If you only want the first occurrence then don't use global flag... it only match the first occurrence . see the following link . Otherwise you can put the global flag 'g'

Explanation

like image 33
Rizwan M.Tuman Avatar answered Mar 03 '23 02:03

Rizwan M.Tuman