enesness
enesness

Reputation: 3133

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 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.

Upvotes: 2

Views: 481

Answers (1)

Tim
Tim

Reputation: 14154

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.

Upvotes: 4

Related Questions