Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion in Java Regular Expression [duplicate]

Tags:

java

regex

What does these following two regular expression means?

.*? and .+?

Actually I understood usage these Quantifiers i.e.

'.' -> Any character
'*' -> 0 or more times
'+' -> once or more times
'?' -> 0 or 1

Indeed, I am literally confused!!! about using .*? and .+?.Could anybody show up with proper examples for these cases.

And you'r most welcome to share good links that presents useful examples practices. Thanks in advance.

like image 636
puru Avatar asked Dec 16 '22 02:12

puru


1 Answers

To break down we have:

. - Any character
* - Any number of times
? - That is consumed reluctantly

. - Any character
+ - At least once
? - That is consumed reluctantly

A reluctant or "non-greedy" quantifier (the '?') matches as little as possible in order to find a match. A more in-depth look at qantifiers (greedy, reluctant and possessive) can be found here

like image 89
Rich O'Kelly Avatar answered Dec 27 '22 21:12

Rich O'Kelly