0

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
4
  • We need more information. Do you want to capture each double? Can you provide a concrete example of the input? As a long time regex user, I'd highly recommend this site, in fact I've set up a saved regex to get you started: regex101.com/r/OcgDLK/2 Commented Aug 11, 2018 at 14:23
  • I want validate for single string value. Commented Aug 11, 2018 at 14:28
  • What is the expected output for the input? What do you want to match? Commented Aug 11, 2018 at 14:28
  • Expected output is boolean = isValid. Commented Aug 11, 2018 at 14:31

2 Answers 2

2

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.

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

6 Comments

I was under the assumption looking at this part |[.][0-9]+ of OP's regex the it should also match for example 0.9
I see your point, the question was rather misleading. In that case OP could use ^(?:(?:^[+-]?|, ?[+-]?)(?:\d+|\.\d+)(?:\.\d+)?){3}$.
I think that would also match .9.9,.9.9,.9.9
Good point, I like that we are trying to break each others patterns ;) ^(?:(?:^[+-]?|, ?[+-]?)(?:\d+(?:\.\d+)?|\.\d+)){3}$ should do the job in that case.
Haha well I am sometimes guilty of it :) Your pattern is longer but according to that engine actually faster, so +1 for you too.
|
2

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}$

2 Comments

@АртемМатюхин it's really frustrating that you won't give us an accurate model for your data. I'm left not knowing whether you're actually matching doubles separated by commas or whether there are literally + and - in the string and I can't be sure whether there should be spaces.
pattern ^[-+]?(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+)(?:,\s*[-+]?(?:[0-9]+(?:\.[0-9]+)?)){0,2}$ give valid for "1,1"

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.