java – Regex to look up word inside tag with CDATA

Question:

I have a file that contains the following string possibilities:

1st Case: <text><![CDATA[Casa]]></text>

2nd Case: <text><![CDATA[Qualquer texto que tenha Casa no meio]]></text>

I'm trying to assemble a regular expression to replace the word Casa by Edifício , but I'm having trouble putting together such an expression, I tried as follows:

String text = "<text><![CDATA[Casa]]></text>";
String regex = "(\\<text><![CDATA[\\(\\w+)(/Casa)(\\w+)(\\]]></text>))";

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);

But this gives me an error when compiling the pattern , is it possible to make a regex that returns these two cases?

Answer:

I suggest the following expression xref : (?<=CDATA\[)(.*?)(?=\])

Implementation example

    String casoUm = "<text><![CDATA[Casa]]></text>";
    String casoDois = "<text><![CDATA[Qualquer texto que tenha Casa no meio]]></text>";
    String regex = "(?<=CDATA\\[)(.*?)(?=\\])";

    Matcher matcher = Pattern.compile(regex).matcher(casoUm);
    if (matcher.find()) {
        System.out.println(casoUm.replaceAll("Casa", "Edifício"));
    }

    matcher = Pattern.compile(regex).matcher(casoUm);
    if (matcher.find()) {
        System.out.println(casoDois.replaceAll("Casa", "Edifício"));
    }

Example: RegEx Example

Scroll to Top