-2

I'm trying to create regex pattern to match String inside XML text. For example:

String to match: A PLP não será fechada

XML Text:

<soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <soap:Fault>
            <faultcode>soap:Server</faultcode>
            <faultstring>A PLP não será fechada, o(s) objeto(s) [PX44258394BR, PX44258388BR, PX44258382BR] já estão vinculados em outra PLP!</faultstring>
            <detail>
                <ns2:SigepClienteException
                    xmlns:ns2="http://cliente.bean.master.sigep.bsb.correios.com.br/">A PLP não será fechada, o(s) objeto(s) [PX44258394BR, PX44258388BR, PX44258382BR] já estão vinculados em outra PLP!
                </ns2:SigepClienteException>
            </detail>
        </soap:Fault>
    </soap:Body>
</soap:Envelope>

I'm having trouble to match using Pattern.matches(regex, stringMessage);

private static final String trackingCodeError1 = ".* A PLP não será fechada, .*";

public boolean execute(String errorMessage) {

    Pattern pattern = Pattern.compile(trackingCodeError);
    return Pattern.matches(trackingCodeError, errorMessage);

}

Please Help.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
tusted
  • 13
  • 1
  • 1
    It's almost impossible to make a regex that will actually correctly find things in X/HTML. You really need a parser for this. Java has one built-in, you should use it. – markspace Dec 05 '19 at 23:44
  • this xml come inside String object, even so do you think is not possible ? – tusted Dec 05 '19 at 23:48
  • 1
    What does "inside a String object" have to do with anything? A parser will read strings just fine. https://docs.oracle.com/javase/tutorial/jaxp/stax/why.html – markspace Dec 05 '19 at 23:50
  • It's because your regex has a space near the beginning where there isn't one in your input string. Change `.* A PLP não será fechada, .*` to `.*A PLP não será fechada, .*` – CAustin Dec 05 '19 at 23:50
  • @markspace I don't think using regex is a problem in this case, since OP is just trying to match the inner text of an element, disregarding all of the actual HTML code. – CAustin Dec 05 '19 at 23:52
  • @ArvindKumarAvinash I'll repost but it takes 90mins to repost – tusted Dec 05 '19 at 23:56
  • 1
    This `Why it's not possible to use regex to parse HTML/XML: a formal explanation in layman's terms` is not a duplicate of this question. That is not a real question, it is a opinion based treatise that should not be used to hide regex parsing of html/XML or any SGML. If that is true, then over 100,000 SO questions need to be hidden as well ! –  Dec 06 '19 at 00:10
  • @ArvindKumarAvinash I tried but is not possible – tusted Dec 06 '19 at 00:11
  • 1
    @tusted While the appropriateness of the duplicate is debatable, what *exactly* you want to have happen is unclear in your question. Please [edit] to clarify what you want to do. For example, for a regex, you need to know what you want to match, but also what you want to reject. You also need to specify exactly what portion of the XML you are really wanting to match and what you are going to do with the match, once the match is made. Right now, you appear to be matching the entire XML contents, if it contains the string you're interested in. Doing that will only tell you if the string exists, – Makyen Dec 06 '19 at 00:26
  • 1
    which is *much* more efficiently accomplished by just directly performing a search for a static string, rather than using a regex. – Makyen Dec 06 '19 at 00:27
  • @ArvindKumarAvinash the question is reopened – tusted Dec 06 '19 at 19:26
  • Please [edit] your question to make it clearer as to what you actually want. Based on the answer you accepted, you are actually wanting to just detect the existence of a specific string within another string (i.e. get a true value if the string you're looking for is there and false if it's not there). Is that correct? – Makyen Dec 08 '19 at 03:04

1 Answers1

0

You can accomplish it simply by using String.contains:

class Main {
    public static void main(String[] args) {
        String xml="<soap:Envelope\n" + 
                "    xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" + 
                "    <soap:Body>\n" + 
                "        <soap:Fault>\n" + 
                "            <faultcode>soap:Server</faultcode>\n" + 
                "            <faultstring>A PLP não será fechada, o(s) objeto(s) [PX44258394BR, PX44258388BR, PX44258382BR] já estão vinculados em outra PLP!</faultstring>\n" + 
                "            <detail>\n" + 
                "                <ns2:SigepClienteException\n" + 
                "                    xmlns:ns2=\"http://cliente.bean.master.sigep.bsb.correios.com.br/\">A PLP não será fechada, o(s) objeto(s) [PX44258394BR, PX44258388BR, PX44258382BR] já estão vinculados em outra PLP!\n" + 
                "                </ns2:SigepClienteException>\n" + 
                "            </detail>\n" + 
                "        </soap:Fault>\n" + 
                "    </soap:Body>\n" + 
                "</soap:Envelope>";
        String searchStr="A PLP não será fechada";
        System.out.println(xml.contains(searchStr));
    }
}

Output:

true
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110