regex – C# Distinguish between first and second match
I am using regex to extract the numbers from a string which contains a range. The range could be "less than x"
, "greater than x"
or "between x and y"
:
"10 - 22"
"< 0,5"
"3,50000 - 11,0"
"< 120000"
"> 12"
Below is the relevant code snippet. In the case of "less than x"
and "greater than x"
I use the RegEx (d*,d*)?(d*)
to capture the integer/decimal.
Low = r.Descr.Contains('>')
? new Quantity {
Value = Convert.ToDecimal(Regex.Match(r.Descr, @"(d*,d*)?(d*)").Value)
}
: r.Descr.Contains('-')
? new Quantity {
Value = Convert.ToDecimal(Regex.Match(r.Descr, @"").Value)
}
: null,
High = r.Descr.Contains('<')
? new Quantity {
Value = Convert.ToDecimal(Regex.Match(r.Descr, @"(d*,d*)?(d*)").Value)
}
: r.Descr.Contains('-')
? new Quantity {
Value = Convert.ToDecimal(Regex.Match(r.Descr, @"").Value)
}
: null,
In the case of "between x and y"
I am having difficulties in constructing a RegEx which would extract the relevant number. Is there a way to do this using RegEx?
Read more here: Source link