2

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?

davis
  • 340
  • 1
  • 4
  • 17

2 Answers2

0

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:

choz
  • 17,242
  • 4
  • 53
  • 73
  • I get a Input string format incorrect error. Because it still has a comma. Did you try your code? I do not need decimals. It should be just an int type. – davis Jun 27 '21 at 05:55
  • I did try the code. Can you post the full string you have? – choz Jun 27 '21 at 06:17
  • 1
    @Davis I tried it, it works fine: https://dotnetfiddle.net/wFH2Hk – Caius Jard Jun 27 '21 at 06:53
  • @Davis ps; check your number is valid (if it starts with a comma for example, you'll get an "Input string was not"..); somewhere, somehow, you're putting garbage in, so you're getting garbage out. You can cater for the "leading comma" case, for example, by adjusting the regex to `(\d[0-9,]*)` – Caius Jard Jun 27 '21 at 07:02
  • @CaiusJard You're right - edited! And thanks for verifying it. – choz Jun 27 '21 at 09:33
0

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
The fourth bird
  • 154,723
  • 16
  • 55
  • 70