3

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())..

2 Answers 2

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

Use non greedy instead of greedy quantifiers.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.