regex just only match first substring
I have a string like this
BALANCE:"5048543747",BALDEFOVD:"5119341413",ACCTNO:"0001000918",
I've using REGEX
(.*?),
Result i've received just the first substring is
BALANCE:"5048543747"
in fact, the result which i want get is the array include
{
BALANCE:"5048543747"
BALDEFOVD:"5119341413"
ACCTNO:"0001000918"
}
Can anyone help me. Many thanks.
EDIT
Code i've using
Pattern pattern = Pattern.compile("(.*?),");
Matcher matcher =pattern.matcher("BALANCE:"5048543747",BALDEFOVD:"5119341413",ACCTNO:"0001000918",");
if (matcher.find())
{
System.out.println("found: " + matcher.group(1));
}
Result i'v received
BALANCE:"5048543747"
Try this code:
String input = "BALANCE:"5048543747",BALDEFOVD:"5119341413",ACCTNO:"0001000918",";
String pattern = "(.*?),";
Pattern r = Pattern.compile(pattern);
List<String> matches = new ArrayList<String>();
Matcher m = r.matcher(input);
while (m.find()) {
matches.add(m.group(1));
}
After seeing one the comments, it might be easier for you to just split the string on comma.
while(matcher.find){
System.out.println("found: " + matcher.group(1));
}
The Matcher in Java can be a bit confusing at first, especially when matching on groups. In the above example, matcher.group(0)
is always the entire regular expression. matcher.group(1)
is matches to the first group you specify in your regex. matcher.group(2)
would return matches to the second group in your regex, if you happened to have one (your example does not). Call matcher.find
to retrieve the next set of matches.
This will be usefull
(w+:"d+")
w+ takes the full word until literal :
then process the literal "
d+ takes the numbers until the next literal "
and you take all the information to match
上一篇: 无结尾分隔符'/'发现错误
下一篇: 正则表达式只匹配第一个子字符串