-1

I want to match a "Double, Double, Double, Double" string using regular expression(for check validation of BorderThickness in a WPF App)

I found many similar answers on the stackoverflow , But none of them not worked for me.

I found [0-9]{4},[0-9]{4} on this page , but it doesn't work because I need - + , . characters in the string.

This is my code:

private static readonly Regex _regex = new Regex("[0-9]{4},[0-9]{4}"); 

public static bool TextIsThickness(string text)
{
    return !_regex.IsMatch(text);
}

Example input string:

-1.4,2.75,0,10

Note: This is not duplicate,I need 4 double numbers that are separated by commas not the same as "Regular expression for double number range validation"

Please tell me how can I do it?

Mehmed
  • 132
  • 4
  • 15
  • Possible duplicate of [Regular expression for double number range validation](https://stackoverflow.com/questions/19632555/regular-expression-for-double-number-range-validation) – Paolo Sep 09 '18 at 11:10
  • @elgonzo , They are not in vain, The main problem is that you compare others with yourself, but people and their level of intelligence and talent, as well as their physical conditions are different, I have a physical disability and studying and even writing and typing is not easy for me . – Mehmed Sep 09 '18 at 11:16
  • @UnbearableLightness , that doesn't woke for my case. I need 4 double like -2,1.5,4,0 – Mehmed Sep 09 '18 at 11:37

3 Answers3

1

You just need to find a regex for one double, then repeat it 4 times with , as separator:

^(?:x,){3}x$

where x is the pattern for 1 double.

From this post, the pattern for one double is:

[+-]?([0-9]*[.])?[0-9]+

So the whole regex will be:

^(?:[+-]?([0-9]*[.])?[0-9]+,){3}[+-]?([0-9]*[.])?[0-9]+$

Demo

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0
(([\+\-]\s?)?\d+(\.\d+)?,){3}([\+\-]\s?)?\d+(\.\d+)?

If I understood correctly, you want to find any sequence: That may or may not start with the + or - operator, that may or may not have a whitespace after the operator (if there is one). Then a sequence of 1 or more numbers, that may be or may be not followed by a literal ., and one or more numbers. All of that 4 times. And the first three all ends with a literal ,.

rnarcos
  • 86
  • 1
  • 2
  • 12
0

To answer your question, you can use the following RegEx:

(?:[+-]?\d+\.?\d*,){3}(?:[+-]?\d+\.?\d*)

It will match: a number starting/not starting with + or - followed by one or more digits, an optional . (dot) followed by zero or more digits ending with a , (comma). This is repeated 3 times and the fourth time it's not ending with a comma. This matches your example.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57