1

I know this is a very simple question but since I am very bad in regex , have to ask below question. I want to write a regex expression in java which will match below two url patterns given :

{contextPath}/javax.faces.resource/**

{contextPath}/rfRes/**

The url will be read from http request object and will be compared using java pattern object like below :

Pattern p = //regex here ;
p.matcher(r.getRequestURL().toString()).matches(); 

Can anybody help me in writing a regex experion for above two urls ?

6
  • Are you matching literal ** in URL? Commented Nov 17, 2014 at 10:52
  • It indicates anything after / .So its a wildcard entry there Commented Nov 17, 2014 at 10:55
  • Do you have a HttpServletRequest reference to be able to get context path? Commented Nov 17, 2014 at 10:59
  • No , we don't have . But do we really need it ? We are not matching for contextPath. It can be anything .What we need to match is path in between. Commented Nov 17, 2014 at 11:04
  • Do you really regex for this? Just use request.getRequestURI() and get first part Commented Nov 17, 2014 at 11:06

2 Answers 2

1

You can use this:

boolean matches = r.getRequestURI().matches(".*?/(javax\.faces\.resource|rfRes)/.*");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your repply. But I suppose , above expression will match exactly for /javax.faces.resource/[any character] . But what is needed is [any character]/javax.faces.resource/[any character]. Does /*(javax.faces.resource|rfRes).* will work ?
1

you can use this :

.*?(?:\/javax\.faces\.resource\/|\/rfRes\/).*$

demo here : http://regex101.com/r/rV5mR8/2

It checks whether the string start with either of the two options which you want.

1 Comment

Thanks aelor for repply . We really don't need contextPath. It can be anything . We just need to check javax.faces.resource/** and /rfRes/**. In that case , do we need contextPath in regex expression ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.