我正在寻找这种格式的 url 的正则表达式:
您可以使用这种通用模式:
^(https?:\/\/)?www\.([\da-z\.-]+)\.([a-z\.]{2,6})\/[\w \.-]+?\.pdf$
Run Code Online (Sandbox Code Playgroud)
或者,如果您想验证该 EXACT url,则可以使用:
^(https?:\/\/)?www\.website\.com\/sample\.pdf$
Run Code Online (Sandbox Code Playgroud)
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to \1 (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
http 'http'
--------------------------------------------------------------------------------
s? 's' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
)? end of \1 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \1)
--------------------------------------------------------------------------------
www 'www'
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
website 'website'
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
com 'com'
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
sample 'sample'
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
pdf 'pdf'
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
Run Code Online (Sandbox Code Playgroud)
爪哇代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String input = "https://www.website.com/sample.pdf";
Pattern pattern = Pattern.compile("^(https?:\\/\\/)?www\\.website\\.com\\/sample\\.pdf$");
Matcher matcher = pattern.matcher(input);
if(matcher.find()) {
System.out.println("Match found");
} else {
System.out.println("NO Match found");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7718 次 |
| 最近记录: |