How to make atomic group work with alternations in java regex? -


i have seen simple demo of atomic group. regex :

a(?>bc|b)d 

i try run on java 7 , doesn't work expected because matches in demo:

pattern pattern = pattern.compile("a(?>bc|b)d");         matcher matcher = pattern.matcher("abd");         system.out.println("matches="+matcher.matches()); 

prints:

matches=true 

but atomic group avoid trying alternatives.

how turn on atomic group in java?

it seems misunderstood example http://www.regular-expressions.info/atomic.html

you need understand purpose of atomic group prevent backtracking impossible change already matched substring new substring. match not found first case of alternation ok test next case of alternation.

so string abd , regex a(?>bc|b)c

regex parts    |  matched string parts ---------------+----------------------              |   (?>bc|b)       |  b - happens because `bc` can't matched next case                 |      alternation inside atomic group used d              |  d 

in case of regex linked article a(?>bc|b)c , string abc get

regex parts    |  matched string parts ---------------+----------------------              |   (?>bc|b)       |  bc <--------+  c              |  nothing - c matched , "possessed" atomic                 |            group `c` can't used here 

and because c

a(?>bc|b)c          ^ 

couldn't matched see false result of "abc".matches("a(?>bc|b)c")


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -