weezle
weezle

Reputation: 81

Replace text with regular expressions under certain condition

I need help with Regular Expressions. I need to replace every link that doesn't have this structure: href="www.domain.com/en/" with href="www.domain.com/en/"

So, if I have this ref="www.domain.com/something"> I want to replace it with href="www.domain.com/en/something"

Can't this be done with regular expressions?

Upvotes: 0

Views: 87

Answers (1)

aioobe
aioobe

Reputation: 420990

Just replace ref="www.domain.com/ with ref="www.domain.com/en/

You don't even need a regular expression for this.

I'm assuming you're just trying to do a search / replace for development purposes. If this is production code, I encourage you to look into HTML parser, such as Jsoup and do a proper parse / traversal / replacement.


As pointed out by anubhava, you may want to avoid having /en/en in the result.

This can be done with regular expressions and negative lookahead. To say "domain not followed by /en" you use domain(?!/en).

Upvotes: 1

Related Questions