1

I'm using this regex to match and replace a part of a string:

(.*)<a href=\\"(.*)\\" class=\\"PopupItemLink\\">(.*)<\\\/a>(.*)

This string is an example: (https://regexr.com/3n1f1)

\n&7This is the alert body\n\nYour name: HAlexTM\nYour ID: 1\nHere the link: <a href=\"test.com\" class=\"PopupItemLink\">Hey<\/a>\n\nThis is a html test: <p>Hey<\/p>\n&8Thu Jun 09 18:07:30 CEST 2016

This part of the string (matched by the RegEX) should be replaced with Hey

<a href=\"test.com\" class=\"PopupItemLink\">Hey<\/a>

So in Java I use this code

if (asit.matches("(.*)<a href=\\\\\"(.*)\\\\\" class=\\\\\"PopupItemLink\\\\\">(.*)<\\\\\\/a>(.*)")) {
    asit.replaceAll("<a href=\\\\\"(.*)\\\\\" class=\\\\\"PopupItemLink\\\\\"", "$1");
    asit.replaceAll(">(.*)<\\\\\\/a>", "$1");
    return asit;
}

But it doesn't return anything, what's the problem?

Lorenzo Lapucci
  • 129
  • 4
  • 12

1 Answers1

0

I've resolved it removing the if block and write just replaceAll(), reassigning the variable value since Strings are immutable (thanks to @PM77-1)

output = output.replaceAll("<a href=\"(.*)\" class=\"PopupItemLink\"", "$1");
output = output.replaceAll(">(.*)<\\/a>", " ($1)");
return output;
Lorenzo Lapucci
  • 129
  • 4
  • 12