fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6.  
  7. public class Test
  8. {
  9. public static void Main()
  10. {
  11. var strs = new[] { "aaa bbbb; Name=John Lewis; ccc ddd; Age=20;", "AAA bbbb; Age=21;" };
  12. var pattern = @"(?:\bName=(?<Name>[^;]+).*?;\s+)?\bAge=(?<Age>\d+)";
  13. foreach (var str in strs)
  14. {
  15. var result = Regex.Match(str, pattern);
  16. if (result.Success)
  17. Console.WriteLine("Name: \"{0}\", Age: \"{1}\"", result.Groups["Name"].Value, result.Groups["Age"].Value);
  18. }
  19. }
  20. }
Success #stdin #stdout 0.03s 30656KB
stdin
Standard input is empty
stdout
Name: "John Lewis", Age: "20"
Name: "", Age: "21"