我有一个匹配某些网址的简单正则表达式,但它运行正常但是我想稍微改进一下,所以它会排除包含某个单词的网址。
我的模板:(http:[A-z0-9./~%]+)
IE:
http://maps.google.com/maps
http://www.google.com/flights/gwsredirect
http://slav0nic.org.ua/static/books/python/
http://webcache.googleusercontent.com/search
http://www.python.org/ftp/python/doc/
http://webcache.googleusercontent.com/search
http://www.python.org/ftp/python/
将上面的网址列表与我的模式匹配,我想优化我的模式以排除包含该字词的网址,例如 google
我尝试使用非捕获组,但没有成功,也许我错过了一些东西。
也许我的描述不清楚。
好吧,我有一个从URL中获取的数据文件,然后我使用我提供的模式提取给出的链接列表但是你可以看到模式正在返回它所做的比我想做的更多的所有链接。所以我想改进它,不给我包含某个词的链接,即:谷歌
因此,在我解析数据而不是返回上面的链接列表之后,它将返回以下内容:
http://slav0nic.org.ua/static/books/python/
http://www.python.org/ftp/python/doc/
http://www.python.org/ftp/python/
感谢所有帮助,谢谢!
答案 0 :(得分:2)
试试这个:
(http:(?![^"\s]*google)[^"\s]+)["\s]
之前发布的解决方案的主要区别在于我控制匹配的长度以进行搜索。
答案 1 :(得分:1)
试试这个:
(http:(?!.*google).*)
编辑:(这有效,经过测试)
public static void main( String[] args ) {
final Pattern p = Pattern.compile( "(http:(?!.*google).*)" );
final String[] in = new String[]{
"http://maps.google.com/maps",
"http://www.google.com/flights/gwsredirect",
"http://slav0nic.org.ua/static/books/python/",
"http://webcache.googleusercontent.com/search",
"http://www.python.org/ftp/python/doc/",
"http://webcache.googleusercontent.com/search",
"http://www.python.org/ftp/python/",
};
for ( final String s : in ) {
final Matcher m = p.matcher( s );
System.out.print( s );
if ( m.find() ) {
System.out.println( " true" );
} else {
System.out.println( " false" );
}
}
}
输出:
http://maps.google.com/maps false
http://www.google.com/flights/gwsredirect false
http://slav0nic.org.ua/static/books/python/ true
http://webcache.googleusercontent.com/search false
http://www.python.org/ftp/python/doc/ true
http://webcache.googleusercontent.com/search false
http://www.python.org/ftp/python/ true
答案 2 :(得分:0)
修改您的正则表达式以捕获主机名并使用.contains()
:
public final class TestMatch
{
private static final List<String> urls = Arrays.asList(
"http://maps.google.com/maps",
"http://www.google.com/flights/gwsredirect",
"http://slav0nic.org.ua/static/books/python/",
"http://webcache.googleusercontent.com/search",
"http://www.python.org/ftp/python/doc/",
"http://webcache.googleusercontent.com/search",
"http://www.python.org/ftp/python/"
);
private static final Pattern p
= Pattern.compile("^http://([^/]+)/");
private static final int TRIES = 50000;
public static void main(final String... args)
{
for (final String url: urls)
System.out.printf("%s: %b\n", url, regexIsOK(url));
long start, end;
start = System.currentTimeMillis();
for (int i = 0; i < TRIES; i++)
for (final String url: urls)
regexIsOK(url);
end = System.currentTimeMillis();
System.out.println("Time taken: " + (end - start) + " ms");
System.exit(0);
}
private static boolean regexIsOK(final String url)
{
final Matcher m = p.matcher(url);
return m.find() && !m.group(1).contains("google");
}
}
示例输出:
http://maps.google.com/maps: false
http://www.google.com/flights/gwsredirect: false
http://slav0nic.org.ua/static/books/python/: true
http://webcache.googleusercontent.com/search: false
http://www.python.org/ftp/python/doc/: true
http://webcache.googleusercontent.com/search: false
http://www.python.org/ftp/python/: true
Time taken: 258 ms