I’m having trouble building a regular expression because I’m really a noob to the subject.
The goal is to validate ‘range’ expressions from the user input, like this:
- 20-30 (between 20 and 30)
- 11,12 (2 and 4)
- -10 (smaller than 10)
- 80- (larger than 80)
or combinations of any number of those, like:
-10,11,12,20-30,80-
I’m already able to parse the different components from a string like this using the following
regular expressions (and calling .GetMatches()):
var rangeRegex = new Regex(@"\d+(\.\d+)?-\d+(\.\d+)?");
var smallerThanRegex = new Regex(@"(?<![\d\.])-\d+(\.\d+)?");
var greaterThanRegex = new Regex(@"\d+(\.\d+)?-(?!\d)");
I’m just not sure on how to combine them into a big Regex that can be used to validate whether a string is a valid ‘range expression’.
Is there anyone who knows how to do this without wasting an entire week on it (like I would)? Thank you very much in advance!
This would be the regex to check the syntax, it has no ideas about the semantics of the string!
See it here on Regexr
But splitting on the commas like Guffa suggested would be the better solution (+1)!