Can you explain this named group start position in a Java regex? -


i'm stuck in weird situation here, trying match optional named group in java (8) regex.

for example, following match happens expected , assertions raise no error:

pattern pat = pattern.compile("begin.+(?<middle>middle).+end"); matcher matcher = pat.matcher("begin middle end");  asserttrue(matcher.find()); assertequals(6, matcher.start("middle")); assertequals(12, matcher.end("middle")); 

however, if change regex to

begin.+(?<middle>(middle)?).+end 

matcher.start("middle") returns 12, if match empty.

if change regex

begin.+(?<middle>middle)?.+end 

matcher.start("middle") returns -1, if there's no match.

i'm wondering if it's possible have optional named groups @ in java.

what mistake making?

it has nothing named groups.

the reason .+ right after begin greedily matches string "middle". named group matches nothing (which it's explicitly allowed to).

the solution make first .+ non-greedy, e.g.:

begin.+?(?<middle>(middle)?).+end 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -