I’m not sure why but for some reason The Regex Split method is going over my head. I’m trying to look through tutorials for what I need and can’t seem to find anything.
I simply am reading an excel doc and want to format a string such as $145,000-$179,999 to give me two strings. 145000 and 179999. At the same time I’d like to prune a string such as '$180,000-Limit to simply 180000.
var loanLimits = Regex.Matches(Result.Rows[row + 2 + i][column].ToString(), @"\d+");
The above code seems to chop '$145,000-$179,999 up into 4 parts: 145, 000, 179, 999. Any ideas on how to achieve what I’m asking?
Regular expressions match exactly character by character (there’s no knowledge of the concept of a “number” or a “word” in regular expressions – you have to define that yourself in your expression). The expression you are using,
\d+, uses the character class\d, which means any digit 0-9 (and+means match one or more). So in the expression$145,000, notice that the part you are looking for is not just composed of digits; it also includes commas. So the regular expression finds every continuous group of characters that matches your regular expression, which are the four groups of numbers.There are a couple of ways to approach the problem.
,in your regular expression, so(\d|,)+, which means match as many characters in a row that are either a digit or a comma. There will be two matches:145,000and179,999, from which you can further remove the commas withmyStr.Replace(",", ""). (DEMO)Regex.Replacewith the expression[^\d-]+– which means match anything that is not a digit or a hyphen – and then replace those with"". Then the result would be145000-179999, which you can split with a simple non-regular-expression split,myStr.Split('-'), to get your two parts. (DEMO)Note that for your second example (
$180,000-Limit), you’ll need an extra check to count the number of results returned fromMatchin the first example, andSplitin the second example to determine whether there were two numbers in the range, or only a single number.