mardi 4 août 2015

Trouble matching a regex part of an XML with java

Hello I got a problem using the regex with Java.

I'm trying to parse this :

*whatever string*
<AttributeDesignator AttributeId="MyIDToParse"
DataType="http://ift.tt/QHQMAU"
Category="theCategoryIWantToParse"
MustBePresent="false"
/>
*whatever string that may contain the same regular expression*

using this code (Pattern + Matcher)

Pattern regex = Pattern.compile("AttributeDesignator +AttributeId=\"(.+)\" +.*Category=\"(.+)", Pattern.DOTALL);
Matcher matcher = regex.matcher(xml);
while (matcher.find()) {
    String ID = matcher.group(1);
    String Category = matcher.group(2);

The output is the following :

group 1 :

MyIDToParse"
    DataType="http://ift.tt/QHQMAU"
    Category="theCategoryIWantToParse"
    MustBePresent="false"
    />
    *whatever string that may contain the same regular expression*

group2 :

theCategoryIWantToParse"
    MustBePresent="false"
    />
    *whatever string that may contain the same regular expression*

I feel like it's a simple thing but I can't find whatever I'm doing wrong.. When I used the regex in a website to test them it works correctly and highlight the right expression from my xml entry.

Aucun commentaire:

Enregistrer un commentaire