West Side
West Side

Reputation: 182

How to compose regex with condition in Java?

Condition:

What I do:

([\s]*\d+(\.{1}\d+)?[\s\,\s]+){7}(\d+(\.{1}\d+)?[\s]*){1}
                    ^ this   ^

That's ok, except for one condition.
On this string I get true, but need false:

String s1 = " 0 , 4.4 3.2,, 4.1      2 4 1 7.7";

I can't do this:

Numbers can be separated by only one comma or no comma, but in this case there must be a space (one or many).

Upvotes: 1

Views: 69

Answers (1)

Gryphon
Gryphon

Reputation: 384

This is the one I came up with for the spaces/commas (also removed some redundant parts):

Pattern.compile("([\\s]*\\d+(\\.\\d+)?(?:\\s*,\\s*|\\s+)){7}(\\d+(\\.\\d+)?[\\s]*)");

Seems to do what you want with the sample you provided, at least. (The use of {1} is generally implied for the places you used it, so I removed those)

Upvotes: 1

Related Questions