开发者

Negation on RegEx?

开发者 https://www.devze.com 2023-02-11 21:24 出处:网络
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.

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

精彩评论

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