2
String text = "ref=\"ar\" alias=\"as sa\" value=\"asa\"";

Actually i want get the value of all the data between the double quotes of ref and alias . Have framed the regular expression too. But the prob i am facing is for alias it is not matching the first double quotes but the last one. I want data only upto the first double quotes

String patternstr="(alias=\".*\")|(ref=\"[[\\w]]*\")";    
String patternstr2Level="\".*\"";

In first matching the two parameter will be acquired and in the second matching data in quotes will be acquired

Current Result:

"ar"

"as sa" value="asa"

Required Result:

"ar"

"as sa"

1
  • Your question has been answered but it seems that your problem might be better addressed with StreamTokenizer. Apr 26, 2011 at 8:27

3 Answers 3

5

You just need to make your match a little bit lazier. I believe that

String patternstr="(alias=\".*?\")|(ref=\".*?\")";

should do the trick. By using .*? instead of just .*, that part of the match becomes lazy. In other words, it will try to match the first double quote that it finds rather than matching as much stuff as possible until it gets to the last double quotes. I tested it in Python and it worked great.

1
  • No problem. If this answer was good enough for you, then go ahead and click the check mark on the left of the question so that this question is marked as answered. Apr 26, 2011 at 15:34
0

Try String patternstr="(alias=\"[\\w\\s]*\")|(ref=\"[[\\w]]*\")";

0
0

match the last one as well but modify the group to be excluded from the result with the ?> modifier.

See this for more info:

http://www.regular-expressions.info/atomic.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.