Rikin Patel
Rikin Patel

Reputation: 9403

Validate Decimal number with Mathematic Operator using RegEx in C#

I want to validate string decimal number with some mathematic operator using Regex like

<=-5.6
<10
>=10
10.5
<=-20.4
2.
-.2
>-5.

Invalid string like

<>5
=>5.6
5.6>=

Upvotes: 0

Views: 89

Answers (1)

Tim S.
Tim S.

Reputation: 56586

This regex passes your test cases:

var regex = new Regex(@"^([<>]=?)?-?(\d+\.?|\d*\.?\d+)$");
bool isMatch = regex.IsMatch(testString);

Upvotes: 3

Related Questions