fork download
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8. string line = "Some () text (1)";
  9. string pattern = @"^(?<text>.+?)(\((?<count>\d+)\))?$";
  10. Match m = Regex.Match(line, pattern);
  11. int count = 0;
  12. string text = "";
  13. if (m.Success)
  14. {
  15. text = m.Groups["text"].Value.Trim();
  16.  
  17. if(m.Groups["count"].Success) {
  18. int.TryParse(m.Groups["count"].Value, out count);
  19. }
  20. }
  21.  
  22. Console.WriteLine("Got count = {0}", count.ToString());
  23. }
  24. }
Success #stdin #stdout 0.06s 37232KB
stdin
Standard input is empty
stdout
Got count = 1