Regex - Get digits after a colon

Viewed 224

I have a regex:

var topPayMatch = Regex.Match(result, @"(?<=Top Pay)(\D*)(\d+(?:\.\d+)?)", RegexOptions.IgnoreCase);

And I have to convert this to int which I did

topPayMatch = Convert.ToInt32(topPayMatchString.Groups[2].Value);

So now...

Top Pay: 1,000,000 then it currently grabs the first digit, which is 1. I want all 1000000. If Top Pay: 888,888 then I want all 888888. What should I add to my regex?

2 Answers

You can use something as simple like @"(?<=Top Pay: )([0-9,]+)". Note that, decimals will be ignored with this regex.

This will match all numbers with their commas after Top Pay:, which after you can parse it to an integer.

Example:

Regex rgx = new Regex(@"(?<=Top Pay: )([0-9,]+)");
string str = "Top Pay: 1,000,000";

Match match = rgx.Match(str);
if (match.Success)
{
    string val = match.Value;
    int num = int.Parse(val, System.Globalization.NumberStyles.AllowThousands);
    Console.WriteLine(num);
}

Console.WriteLine("Ended");

Source:

If you use the lookbehind, you don't need the capture groups and you can move the \D* into the lookbehind.

To get the values, you can match 1+ digits followed by optional repetitions of , and 1+ digits.

Note that your example data contains comma's and no dots, and using ? as a quantifier means 0 or 1 time.

(?<=Top Pay\D*)\d+(?:,\d+)*

The pattern matches:

  • (?<=Top Pay\D*) Positive lookbehind, assert what is to the left is Top Pay and optional non digits
  • \d+ Match 1+ digits
  • (?:,\d+)* Optionally repeat a , and 1+ digits

See a .NET regex demo and a C# demo

string pattern = @"(?<=Top Pay\D*)\d+(?:,\d+)*";
string input = @"Top Pay: 1,000,000
Top Pay: 888,888";
RegexOptions options = RegexOptions.IgnoreCase;

foreach (Match m in Regex.Matches(input, pattern, options))
{
    var topPayMatch = int.Parse(m.Value, System.Globalization.NumberStyles.AllowThousands);
    Console.WriteLine(topPayMatch);
}

Output

1000000
888888
Related