java - Regex is returning undesired results -


i trying capture group regex follow pattern:

ex1 - anyanyany group 1 have anyanyany

ex2 - anyanyany.abcany group 1 have anyanyany

ex3 - anyany.abcde.fghi group 1 have anyany.abcde

when try (.+)(?:\.), returns ex2 , ex3. if change (.+)(?:\.)* returns same string of input.

i don't know have solve it. me? knowledgement missing?

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

try non-greedy regex.

(.+?)(?:\.[^.]*)?$ 

in java need escape backslash 1 more time, like,

pattern p = pattern.compile("(.+?)(?:\\.[^.]*)?$"); 

demo


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -