[英]Regex to Exclude only certain characters
I have set of url like this, 我有这样的网址集,
www.abc.com/some-text/
www.xyz.com/some-text/
www.pqr.com/page/2/
I need get URl expect the url containing the word "page" My regex is .*/(.(?!page)).*
IT is not working.can anyone point me the problem and solution for this? 我需要得到URl,期望包含单词“ page”的url我的正则表达式是
.*/(.(?!page)).*
IT无法正常工作。有人可以指出我的问题和解决方案吗?
Why are you looking for regex? 您为什么要寻找正则表达式? This can be done using
String.contains(String s)
这可以使用
String.contains(String s)
String string ="www.pqr.com/page/2/";
if(string.contains("page")){
//true
}
Use following regular expression. 使用以下正则表达式。 (Specify
^
, $
to make sure no character is followed by page
). (指定
^
和$
以确保page
没有字符)。
"^(.(?!\\bpage\\b))+$"
String pattern = "^(.(?!\\bpage\\b))+$";
System.out.println("www.abc.com/some-text/".matches(pattern)); // true
System.out.println("www.xyz.com/some-text/".matches(pattern)); // true
System.out.println("www.pqr.com/page/2/".matches(pattern)); // false
Did you mean except or expect on your question?? 您的意思是“是”还是“不是”?
You can ignore urls having page
in it by lookahead
option. 您可以通过
lookahead
选项忽略其中包含page
网址。
/^(?!.*page).*/
If you want to pick urls having page in it, then 如果您要选择包含页面的网址,则
/^(?=.*page).*/
Use URI
: 使用
URI
:
public boolean containsPage(final String input)
{
return URI.create(input).getPath().contains("page");
}
This allows to search for page
only in the path component and will not be fooled if present in the host name/query string/fragment part. 这允许仅在路径组件中搜索
page
,并且如果存在于主机名/查询字符串/片段部分中,则不会被愚弄。
You can use REGEX : 您可以使用REGEX:
(^(?:.(?!\bpage\b))+$)
CODE : 代码:
String regex="(^(?:.(?!\\bpage\\b))+$)";
String lines[]={
"www.abc.com/some-text/",
"www.xyz.com/some-text/",
"www.pqr.com/page/2/"
};
for(String line:lines){
if(line.matches(regex)){
System.out.println(line);
}
}
OUTPUT : 输出:
www.abc.com/some-text/
www.xyz.com/some-text/
EXPLANATION 说明
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.