Neil
Neil

Reputation: 332

Regex with matcher returning only the first result

Hello I got a problem using the regex with Java.

I'm trying to parse this :

*whatever string*
<AttributeDesignator AttributeId="MyIDToParse"
DataType="http://www.w3.org/2001/XMLSchema#string"
Category="theCategoryIWantToParse"
MustBePresent="false"
/>
*whatever string ***that may contain the same regular expression again*** *

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);

My problem is that my regexp returns only the first occurence of the pattern, even if I have a while(matcher.find())..

Upvotes: 3

Views: 310

Answers (2)

Bashir Mahmoudi
Bashir Mahmoudi

Reputation: 176

Your problem is *Category=\"(.+) catcches all the remaining text. You must close it as Category=\"(.+)\".

Upvotes: 0

vks
vks

Reputation: 67968

Pattern regex = Pattern.compile("AttributeDesignator +AttributeId=\"(.+?)\" +.*?Category=\"(.+?)", Pattern.DOTALL);

Use non greedy instead of greedy quantifiers.

Upvotes: 3

Related Questions