开发者

Negation on RegEx?

I have such string

​String str = "<img src='earth'> ddd earth ggg earth. fff "

I want to replace all 'earth' by 'world' except the one in img's src. Namely I want to get the string

<img src='earth'> ddd world ggg world. fff

Probably I need an intelligent regex to detect开发者_如何学Go if the word is in src but could not find a way to do it. Or may be negation may help.

Thanks for your help.


If the string is well-formed you could use a negative look-behind.

s/(?<!src=')earth/world/

The (?<!...) construct is called a negative look-behind and matches as long as its content is not there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜