简体   繁体   English

正则表达式以匹配网址格式

[英]Regex to match url patterns

I know this is a very simple question but since I am very bad in regex , have to ask below question. 我知道这是一个非常简单的问题,但是由于我在regex中非常不好,所以必须问以下问题。 I want to write a regex expression in java which will match below two url patterns given : 我想在Java中编写一个正则表达式,它将匹配下面给出的两个URL模式:

{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 : 该网址将从http请求对象中读取,并使用如下所示的java模式对象进行比较:

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

Can anybody help me in writing a regex experion for above two urls ? 有人可以帮我写两个以上网址的正则表达式吗?

you can use this : 您可以使用此:

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

demo here : http://regex101.com/r/rV5mR8/2 演示在这里: http : //regex101.com/r/rV5mR8/2

It checks whether the string start with either of the two options which you want. 它检查字符串是否以所需的两个选项之一开头。

您可以使用此:

boolean matches = r.getRequestURI().matches(".*?/(javax\.faces\.resource|rfRes)/.*");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM