fork 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 strings = new List<string> { "JohnnameSnow", "John" };
  12. foreach (var s in strings)
  13. {
  14. Console.WriteLine(s);
  15. var m = Regex.Match(s, @"^(?<first>\p{L}+?)(?:name(?<last>\p{L}+))?$");
  16. if (m.Success)
  17. {
  18. Console.WriteLine("First name: {0}, Last name = {1}", m.Groups["first"].Value, m.Groups["last"].Value);
  19. }
  20. else
  21. {
  22. Console.WriteLine("No match!");
  23. }
  24. }
  25. }
  26. }
Success #stdin #stdout 0.06s 21292KB
stdin
Standard input is empty
stdout
JohnnameSnow
First name: John, Last name = Snow
John
First name: John, Last name =