Home > DeveloperSection > Forums > Replacing link tag using regex
Ida B Wells
Ida B Wells

Total Post:30

Posted on    October-14-2013 3:42 AM

 Java Java 

 1 Reply(s)
 977  View(s)
Rate this:

I'm using java. And i want to find then replace hyperlink and anchor text of tag <a> html. I knew i must use: replace() method but i'm pretty bad about regex. An example:

<a href="http://example.com">anchor text 1</a>

will be replaced by:

<a href="http://web.com">anchor text 2</a>

Could you show my the regex for that purpose?

Thanks a lot.

Garry Winogrand
Garry Winogrand

Total Post:24

Posted on    October-14-2013 4:09 AM

You could perhaps use a replaceAll with the regex:

<a href=\"[^\"]+\">[^<]+</a>

And replace with:

<a href=\"http://web.com\">anchor text 2</a>

[^\"]+ and [^<]+ are negated class and will match all characters except " and < respectively.

Don't want to miss updates? Please click the below button!

Follow MindStick