1

I have a couple of different string-numeric patterns that needs validation during user input.

They look like this

// Always a dash, always a comma
var variant1 = "0-5,99";

// Always a dash, never a comma, always integers
var variant2 = "10-20";

I was thinking of doing something like this but obviously it doesn't work :/

var reg1 = @"(\w{2})-(\w{2})";
var match = Regex.IsMatch(variant2 , reg1);

Thanks

2 Answers 2

3
var reg1 = @"^(?:\d+\-\d+,\d+)|(?:\d+\-\d+)$";

This looks for one of two groups, either matching "digit(s) dash digits(s) comma digit(s)"

^\d+\-\d+,\d+$

or matching "digit(s) dash digits(s)"

^\d+\-\d+$

and it uses ^ and $ to force a "whole string" match.

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

Comments

0

"[0-9]+-[0-9]+" ist at least one number, followed by the dash and at least one more number. (May be escape the middle "-", if it does not work)

1 Comment

This would still match 0-5.99 as true!

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.