Reputation: 201
I'm trying to match the following string against the pattern:
String s = "AAA|VY~1055~ ~~BCN~09/24/2012~";
Matcher m = Pattern.compile("(.*)\\|VY\\~(.*)\\~").matcher(s);
if (m.find())
{
String value = m.group(2);
System.out.print("value = " + value);
}
The output is:
value = 1055~ ~~BCN~09/24/2012
But I want this:
value = 1055
Why is it getting all the characters until the end of string?
I've read something about to consume to the end of string, and I've tried:
Matcher m = Pattern.compile("(.*)\\|VY\\~(.*)\\~(.*)").matcher(s);
Matcher m = Pattern.compile("(.*)\\|VY\\~(.*)\\~.*").matcher(s);
But it doesn't work.
Can anybody help me?
Upvotes: 0
Views: 53
Reputation: 22070
You want to read about the gready, reluctant and possessive quantifiers (need to scroll down a bit).
Upvotes: 0
Reputation: 97571
Use the *?
(reluctant) quantifier, which is lazy (stops matching as soon as possible).
Matcher m = Pattern.compile("(.*)\\|VY\\~(.*?)\\~").matcher(s);
Upvotes: 2