java - Why does the look-behind expression in this regex not have an "obvious maximum length"? -
given string containing number of square brackets , other characters, want find closing square brackets preceded opening square bracket , number of letters. instance, if string is
] [abc] [123] abc]
i want find second closing bracket.
the following regex
(?<=[a-z]+)\]
will find me second closing bracket, last one:
] [abc] [123] abc]
since want find first one, make obvious change regex...
(?<=\[[a-z]+)\]
...and "look-behind group not have obvious maximum length near index 11."
\[
single character, seems obvious maximum length should 1 + whatever obvious maximum length of look-behind group in first expression. gives?
eta: it's not specific opening bracket.
(?<=a[b-z]+)\]
gives me same error. (well, @ index 12.)
\[ single character, seems obvious maximum length should 1 + whatever obvious maximum length of look-behind group in first expression. gives?
that's point, "whatever obvious maximum length of look-behind group in first expression", not obvious. rule of fist can't use +
or *
inside look-behind. not java's regex engine, many more pcre-flavored engines (even perl's (v5.10) engine!).
you can look-aheads however:
pattern p = pattern.compile("(?=(\\[[a-z]+]))"); matcher m = p.matcher("] [abc] [123] abc]"); while(m.find()) { system.out.println("found ']' before index: " + m.end(1)); }
(i.e. capture group inside ahead (!) can used end(...)
of group)
will print:
found ']' before index: 7
edit
and if you're interested in replacing such ]
's, this:
string s = "] [abc] [123] abc] [foo] bar]"; system.out.println(s); system.out.println(s.replaceall("(\\[[a-z]+)]", "$1_"));
which print:
] [abc] [123] abc] [foo] bar] ] [abc_ [123] abc] [foo_ bar]
Comments
Post a Comment