-1

I need a regex string to match URL starting with "https" "www.mydomain.com", "myView?" "rest of the url" The regex is for use in the jsf views no for code.

Valid urls:

https://www.mydomain.com/myView?z
https://www.mydomain.com/myView?a=hello&b=world&c=how&d=are&e=you
https://www.mydomain.com/myView?a=hello&b=world&c=how&d=are&e=(you)

Invalid urls:

https://www.mydomain.com/myView?
https://www.mydomain.com/myView
https://www.mydomain.com/myViewfdgdfgfd

Test code:

//Pattern to check if this is a valid URL address, not work
Pattern p = Pattern.compile("^(https://www.mydomain.com/myView?)");
Matcher m;
m=p.matcher(urlAddress);
xav56883728
  • 315
  • 1
  • 8
  • 20

1 Answers1

0

It's a simple case. This only have two parts, the start of the expression and the rest.

Start expression:

^https://www.mydomain.com/myView\\?

Rest of the url (Valid url chars):

[a-zA-Z0-9~!';@#\\^\\$%&\\*\\(\\)-_\\+=\\[\\]\\{\\}\\|\\\\,\\.\\?\\s]

Test code:

    String regex = "^https://www.mydomain.com/myView\\?[a-zA-Z0-9~!';@#\\^\\$%&\\*\\(\\)-_\\+=\\[\\]\\{\\}\\|\\\\,\\.\\?\\s]+";

    String validUrlchars = "[a-zA-Z0-9~!';@#\\^\\$%&\\*\\(\\)-_\\+=\\[\\]\\{\\}\\|\\\\,\\.\\?\\s]";

    String url1 = "https://www.mydomain.com/myView?z";
    String url2 = "https://www.mydomain.com/myView?";
    String url3 = "https://www.mydomain.com/myView";
    String url4 = "https://www.mydomain.com/myViewfdgdfgfd";
    String url5 = "https://www.mydomain.com/myView?a=hello&b=world&c=how&d=are&e=you";
    String url6 = "https://www.mydomain.com/myView?a=hello&b=world&c=how&d=are&e=(you)";

    Pattern p = Pattern.compile(regex);
    Matcher m;
    m = p.matcher(url1);
    System.out.println(m.matches() + " " + url1);
    m = p.matcher(url2);
    System.out.println(m.matches() + " " + url2);
    m = p.matcher(url3);
    System.out.println(m.matches() + " " + url3);
    m = p.matcher(url4);
    System.out.println(m.matches() + " " + url4);
    m = p.matcher(url5);
    System.out.println(m.matches() + " " + url5);
    m = p.matcher(url6);
    System.out.println(m.matches() + " " + url6);

Output:

true https://www.mydomain.com/myView?z
false https://www.mydomain.com/myView?
false https://www.mydomain.com/myView
false https://www.mydomain.com/myViewfdgdfgfd
true https://www.mydomain.com/myView?a=hello&b=world&c=how&d=are&e=you
true https://www.mydomain.com/myView?a=hello&b=world&c=how&d=are&e=(you)
Community
  • 1
  • 1
xav56883728
  • 315
  • 1
  • 8
  • 20