<img src='http://www.divshare.com/img/4415865-245' <img src='http://i218.photobucket.com/albums/cc151/yuuwill/html.gif' align='top'><img src='(.*?)([^gif']?')我要匹配 不是以 gif' 结尾的 上面的表达式怎么还是匹配了 'http://i218.photobucket.com/albums/cc151/yuuwill/html.gif 了啊
import java.util.regex.Pattern;public class Test1 {
public static void main(String[] args) {
String str = "<img src='http://www.divshare.com/img/4415865-245'" +
"<img src='http://i218.photobucket.com/albums/cc151/yuuwill/html.gif' align='top'>";
Pattern pattern = Pattern.compile("<img src='([^']*)(?<!gif)'");
Matcher matcher = pattern.matcher(str);
while(matcher.find()) {
System.out.println(matcher.group());
}
}
}
(?<!gif)这里的 ! 号是什么意思
以及要达到的效果,说得越详细正则表达式的匹配完整性就越好。
假如一个字符串中有N个 <br>
但我只匹配最后一个 <br>
怎么写表达式呢
——————————————————
<br> 是连续着的呢,还是其间夹有其他的标记或者空格什么的,都没有说明。像你这个就好比在问我要写一个操作系统该怎么做啊,跟没说一样。
也叫做 零宽断言,意思是匹配前面不是gif的位置
也就是简单的说,如果字符串中如果有以gif结尾的,那就不匹配这个字符串
str = "aaaaaaaaaaaa<br>bbbbbbbbbbbbbbbbbbbbbbbbewe<br>dsieworiew<br>";
String regx = "<([^>]*)(?<!br?)>";
Pattern p = Pattern.compile(regx, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(str);
while (m.find()) {
str = str.replace(m.group(), "");
}
得到的结果怎么是ttp://www.jandown.com/link.php?ref=ShLhqsIAey
怎么把 h 也给匹配了呢
为什么不是你这个结构呢?
我看了下。。就是你这个结果没有意外阿
import java.util.regex.*;
public class RegularONe
{
public static void main(String[] args)
{
String resource= " <img src='http://i218.photobucket.com/albums/cc151/yuuwill/torrent.gif'" +
" align='absmiddle'> h <span style='font-size:1px'> " +
"</span>ttp://www. <span style='font-size:1px'> " +
"</span>jandown. <span style='font-size:1px'> " +
" </span>com/link. <span style='font-size:1px'> " +
" </span>php?ref=ShLhqsIAey </a>";
String regularexpression="<([^>]*)(?<!br?)>";
Pattern pattern=Pattern.compile(regularexpression);
Matcher matcher=pattern.matcher(resource);
while(matcher.find())
{
resource=resource.replace(matcher.group(), "");
}
System.out.println(resource.trim());
}}结果
h ttp://www. jandown. com/link. php?ref=ShLhqsIAey
* 匹配匹配并提取url <br>
*
* 格式: XXXX://XXX.XXX.XXX.XX/XXX.XXX?XXX=XXX
*
* 匹配 : http://www.suncer.com 或news://www<br>
*
* 提取(MatchResult matchResult=matcher.getMatch()): matchResult.group(0)=
* http://www.suncer.com:8080/index.html?login=true matchResult.group(1) =
* http matchResult.group(2) = www.suncer.com matchResult.group(3) = :8080
* matchResult.group(4) = /index.html?login=true
*
* 不匹配: c:\window
*
*/
public static final String url_regexp = "(\\w+)://([^/:]+)(:\\d*)?([^#\\s]*)";