0

I have a string with data separated by commas like this:

$d4kjvdf,78953626,10.0,103007,0,132103.8945F,

I tried the following regex but it doesn't match the strings I want:

[a-zA-Z0-9]+\\,[a-zA-Z0-9]+\\,[a-zA-Z0-9]+\\,[a-zA-Z0-9]+\\,[a-zA-Z0-9]+\\,[a-zA-Z0-9]+\\,
5
  • 2
    What defines a valid or invalid string? Commented Aug 26, 2014 at 16:09
  • @dimo414 number of commas Commented Aug 26, 2014 at 16:11
  • So any letters, other than commas, are allowed? Commented Aug 26, 2014 at 16:12
  • yes any letters other than commas allowed Commented Aug 26, 2014 at 16:14
  • Does your string need to have comma at the end? Or I will ask differently: is position of commas relevant? Commented Aug 26, 2014 at 16:15

2 Answers 2

2

The $ at the beginning of your data string is not matching the regex. Change the first character class to [$a-zA-Z0-9]. And a couple of the comma separated values contain a literal dot. [$.a-zA-Z0-9] would cover both cases. Also, it's probably a good idea to anchor the regex at the start and end by adding ^ and $ to the beginning and end of the regex respectively. How about this for the full regex:

^[$.a-zA-Z0-9]+\\,[$.a-zA-Z0-9]+\\,[$.a-zA-Z0-9]+\\,[$.a-zA-Z0-9]+\\,[$.a-zA-Z0-9]+\\,[$.a-zA-Z0-9]+\\,$

Update:

You said number of commas is your primary matching criteria. If there should be 6 commas, this would work:

^([^,]+,){6}$

That means: match at least 1 character that is anything but a comma, followed by a comma. And perform the aforementioned match 6 times consecutively. Note: your data must end with a trailing comma as is consistent with your sample data.

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

Comments

0

Well your regular expression is certainly jarbled - there are clearly characters (like $ and .) that your expression won't match, and you don't need to \\ escape ,s. Lets first describe our requirements, you seem to be saying a valid string is defined as:

A string consisting of 6 commas, with one or more characters before each one

We can represent that with the following pattern:

(?:[^,]+,){6}

This says match one or more non-commas, followed by a comma - [^,]+, - six times - {6}. The (?:...) notation is a non-capturing group, which lets us say match the whole sub-expression six times, without it, the {6} would only apply to the preceding character.


Alternately, we could use normal, capturing groups to let us select each individual section of the matching string:

([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),?

Now we can not only match the string, but extract its contents at the same time, e.g.:

String str = "$d4kjvdf,78953626,10.0,103007,0,132103.8945F,";
Pattern regex = Pattern.compile(
  "([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),?");
Matcher m = regex.matcher(str);

if(m.matches()) {
  for (int i = 1; i <= m.groupCount(); i++) {
    System.out.println(m.group(i));
  }
}

This prints:

$d4kjvdf
78953626
10.0
103007
0
132103.8945F

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.