1
votes

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"

3
Could you add the code you are using? - Vlad Călin Buzea
Works fine for me. Please post your code. - shmosel
Why not just split(",") ? - AJ.
Hix. I'm so idiot. So ez to fix it. Thanks AJ :) - Lộc Nguyễn

3 Answers

1
votes

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.

0
votes
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.

0
votes

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