user3872094 user3872094 - 14 days ago 6
Java Question

Unable to know how to use the substitution

I've a string like below

<a name="1.F"></a>


I'm trying to extract only the value name value. And the regex I'm using to get this done is.

Search string

<a name=\"(\d)+(\.)(\w)+\"?>


Replace String

$1$2$3


Result: Here the Result that I get is what is in between **

search result :

**<a name="1.F">**</a>


Current Replace Result:

1.F</a>


Expected Replace Result:

1.F


please let me know, how can we get the Expected result.

Thanks

Answer

I think you should change it to

<a name=\"(\d)+(\.)([^"]+)\"?>

replace (\w) by ([^"]) .

    String s = "**<a name=\"1.F\">**</a>";
    Pattern p = Pattern.compile("<a name=\\\"(\\d)+(\\.)([^\"]+)\\\"?>");
    Matcher m = p.matcher(s);
    while (m.find()) {
        System.out.println(m.group(1) + m.group(2) + m.group(3));
    }

=>> 1.F