fork download
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8. string[] strings = { "/api/ping.json",
  9. "/api/ping",
  10. "/api/ping/xxx/sss.json",
  11. "/api/ping.xml",
  12. "/api/ping/.json",
  13. "/api/pingpong",
  14. "/api/ping_pong",
  15. "/api/ping-pong"
  16. };
  17. string pattern = @"/api/ping(?:(?:/[^/\s]+)*\.\S+|$)";
  18. foreach (String s in strings) {
  19. if (Regex.IsMatch(s, pattern)) {
  20. Console.WriteLine("Valid: {0}", s);
  21. } else {
  22. Console.WriteLine("Invalid: {0}", s);
  23. }
  24. }
  25. }
  26. }
Success #stdin #stdout 0.03s 134592KB
stdin
Standard input is empty
stdout
Valid: /api/ping.json
Valid: /api/ping
Valid: /api/ping/xxx/sss.json
Valid: /api/ping.xml
Invalid: /api/ping/.json
Invalid: /api/pingpong
Invalid: /api/ping_pong
Invalid: /api/ping-pong