regex - Java Matcher.replaceAll() matches group(0) elements as well -
i think common problem, couldn't find related question in or other tutorial well. if it's duplicate, feel free mark such.
well here problem, have string , replace words test in tested. don't want replace word if it's contained inside <>
eg: string this test <other test end> should replaced this tested <other test end>.
so have create regex (?:<.*?>)|(test) (anything inside <> should non-capturing group , other test should match.
but understand matcher.group(0) match non-capturing group. , what's worse matcher.replaceall() seems replace non-capturing group text well?
how can solve this? there way can specify replaceall() replace group(1) elements?
is there other easy , clean way solve problem?
use negative lookahead assertion.
string.replaceall("test(?![^<>]*>)", "tested") explanation:
test- matches string test if it's not followed byany char not of
<or>, 0 or more times.further followed
>char. matchestestexcept 1 present inside<>
Comments
Post a Comment