简体   繁体   中英

Java Regex Pattern Matcher Replace Group with repetition

I have the following scenario, I need to change,

<a href="ab/xyz" onclick="ab/123"></a>

to

<a href="pq/xyz" onclick="pq/123"></a>

basically replace "ab" with "pq", whenever "ab" appears in attribute values of a html tag

I wrote the following regex,

(<[^>]+)((=")(ab)([^>/"]*"))+([^>].*>)

and I am doing replaceAll

if(matcher.find())
matcher.ReplaceAll($1$3pq$4$5)

The above code only replaces one attribute value per tag even though I have repetition operator in my regex and I am doing ReplaceAll

If I change the "if" condition to while loop, then it changes all attributes, basically 1 attribute per iteration

Is there a way to just replace all matches in all attribute values without a loop?

Solution: A dumb regex is doing the trick even without repetition operator. Problem was I was matching the entire tag.

It replaces only one occurence, because the .* at the end matches the entire length of your stirng (well, everything up to the last > , but most likely that is the end of the document since it'll end with html> ) - and there is no other match behind that.

Java supports lookaheads and lookbehinds, we'll need those to make it work. Basically, a lookahead tells Java to "only match if the match is followed by whatever, but whatever is not part of the match itself". Lookbehinds are ther same, just that whatever has to precede the match. Unfortunately Java doesn't support * and + inside lookbehinds, so they're a little tricky, but it should work:

([^>]*?="[^"]*?)ab(?=[^<]*>)

replace it by $1pq .

I tested it, it works - but only replaces one ab inside each attribute (the first one). If you have multiple ab s in one attribute and all shoudl be replaced, I see no way (without proper lookbehinds)

Note that this is assuming valid HTML - it may yield unexpected results on invalid HTML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM