Reputation: 141
I have a line of pattern:
double1, +double2,-double3
.
For single double value pattern is :
[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)
How to make it for triple value? Such as:
1.1, 0, -0
0, -123, 33
Not valid for:
""
1,123
123,123,123,123
Upvotes: 0
Views: 49
Reputation: 26163
You can use a slightly simpler pattern:
^(?:(?:^[+-]?|, ?[+-]?)\d+(?:\.\d+)?){3}$
Matches only triple occurences as you specified in your edit. You can try it here.
As correctly pointed out by The Fourth Bird in his comments below, if you wish to match entries such as .9
, where no digits precede the full stop you can use:
^(?:(?:^[+-]?|, ?[+-]?)(?:\d+(?:\.\d+)?|\.\d+)){3}$
You can check this pattern here.
Upvotes: 2
Reputation: 163467
The double part ([.][0-9]*)?
is optional which will match 0 or 1 times.
To match it triple times, you could match a double using [-+]?(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+)
which will match an optional +
or -
followed by an alternation that will match either a digit followed by an optional part that matches a dot and one or more digits or a dot followed by one or more digits.
Repeat that pattern 2 times using a quantifier {2}
preceded by a comma and zero or more times a whitespace character \s*
.
Add anchors to assert the start ^
and the end $
of the string and you could make use of a non capturing group (?:
if you only want to check if it is a match and not refer to the groups anymore.
^[-+]?(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+)(?:,\s*[-+]?(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+)){2}$
Upvotes: 2