0

New to using C# Regex, I am trying to capture two comma separated integers from a string into two variables.

Example: 13,567

I tried variations on

Regex regex = new Regex(@"(\d+),(\d+)");
var matches = regex.Matches("12,345");

foreach (var itemMatch in matches)
    Debug.Print(itemMatch.Value);

This just captures 1 variable, which is the entire string. I did workaround this by changing the capture pattern to "(\d+)", but that then ignores the middle comma entirely and I would get a match if there were any text between the integers. How do I get it to extract both integers and ensure it also sees a comma between.

depperm
  • 10,606
  • 4
  • 43
  • 67
geoffw123
  • 83
  • 5

2 Answers2

3

Can do this with String.Split

Why not just use a split and parse?

var results = "123,456".Split(',').Select(int.Parse).ToArray();
var left = results[0];
var right = results[1];

Alternatively, you can use a loop and use int.TryParse to handle failures but for what you're looking for this should cover it

If you're really committed to a Regex

You can do this with a Regex too, just need to use groups of the match

Regex r = new Regex(@"(\d+)\,(\d+)", RegexOptions.Compiled);
var r1 = r.Match("123,456");
    
//first is total match
Console.WriteLine(r1.Groups[0].Value);
    
//Then first and second groups
    
var left = int.Parse(r1.Groups[1].Value);
var right = int.Parse(r1.Groups[2].Value);
    
Console.WriteLine("Left "+ left);
Console.WriteLine("Right "+right);

Made a dotnetfiddle you can test the solution in as well

Community
  • 1
  • 1
konkked
  • 3,161
  • 14
  • 19
  • 2
    The question is not about how to split a string, but about how to access capture groups. Everyone knows how to split a string. – Wiktor Stribiżew Jul 20 '16 at 13:24
  • @WiktorStribiżew question was about a Regex but the solution was obtainable with a split, added how to accomplish what OP was looking for wth a Regex as well – konkked Jul 20 '16 at 13:35
0

With Regex, you can use this:

Regex regex = new Regex(@"\d+(?=,)|(?<=,)\d+");
var matches = regex.Matches("12,345");

foreach (Match itemMatch in matches)
    Console.WriteLine(itemMatch.Value);

prints:

12
345

Actually this is doing a look-ahead and look-behind a , :

\d+(?=,)     <---- // Match numbers followed by a ,
|            <---- // OR
(?<=,)\d+    <---- // Match numbers preceeded by a ,
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • 1
    The point is not how to write a "better" regex, but how to access captured values. – Wiktor Stribiżew Jul 20 '16 at 13:24
  • Eugh, smelling salts required pleased ! For what I thought was a simple problem, that is a horrific looking regex template for a newb to understand. – geoffw123 Jul 20 '16 at 13:30
  • @geoffw123 don't think it is helpful to make comments like that personally, could simply say that it looks over complicated...that being said do agree that this looks like a sub-optimal way to handle the problem – konkked Jul 20 '16 at 13:33
  • @geoffw123 I added a description. – Zein Makki Jul 20 '16 at 13:33
  • Tech topics are a bit dry to read and regex questions are particularly bad I think, so a bit of polite levity and humour is no bad thing. I was impressed by the posters Regex "Ninja" ness, but the template still gave me a headache. Thanks for the replies guys, the word "group" was enough for me to solve it myself. I was a bit surprised by the fact that group index[0] returns the entire string. That was non intuitive, wonder why it does that ? – geoffw123 Jul 20 '16 at 13:51
  • P.S thanks for the extra explanation of that template. My headache is wearing off a bit :) – geoffw123 Jul 20 '16 at 14:03