Wednesday, May 28, 2014

Building a Complex Regular Expressions


I follow this way to build a very complex regular expression. But this has to be start from very small
  • /v1/user  -  /(\\w*)/(\\w*)
  • v1/203i03 -  /(\\w*)/(\\w*)
  • v1 -   /(\\w*)/*(\\w*)

  • /v1/user?name  -  /(\\w*)/(\\w*)(\\?*)(\\w*)
  • /v1/user?name=don  -  /(\\w*)/(\\w*)(\\?*)(\\w*)(\\=*)(\\w*)


Java Code to Test the regular expression


private static void getEndPointUrl(String restUrlPattern,String urlToMatch) {
Pattern pattern = Pattern.compile(restUrlPattern);
Matcher matcher = pattern.matcher(urlToMatch);
System.out.println(restUrlPattern + " -- " +urlToMatch);
if (matcher.matches()) {
System.out.println("Matches");;
} else {
System.out.println("Not Matches");;
} }

    No comments:

    Post a Comment