java里正则表达式这样用不对么?
得一高手指点
我想把 <img alt= "tyui " border= "0 " src= "http://www.sina.com.cn ">
换成 <img alt= "tyui " border= "0 " src= "site/img ">
用js就可以
<script type= "text/javascript ">
var s= " <img alt=\ "tyui\ " border=\ "0\ " src=\ "http://www.sina.com.cn\ "> ";
var regS=/(src=)\ "(.*?)\ "/;
s.match(regS);
alert(s.replace(RegExp.$2, "site/img "));
</script>
可是我放到java里
import java.util.regex.*;
class b{
public static void main(String args[]) {
String regEx= "/(src=)\ "(.*?)\ "/ ";
String str= " <img alt=\ "tyui\ " border=\ "0\ " src=\ "http://www.sina.com.cn\ "> ";
Pattern p=Pattern.compile(regEx);
Matcher m=p.matcher(str);
String s=m.replaceAll( "site/img "); // ( " ") 删除
System.out.println(s);
}
}
输出的结果还是
<img alt= "tyui " border= "0 " src= "http://www.sina.com.cn ">
哪里不对了?盼高手指点阿
------解决方案-------------------- public static void main(String[] args) {
String regEx= "(? <=(src=))\ "(.*)\ " ";
String str= " <img alt=\ "tyui\ " border=\ "0\ " src=\ "http://www.sina.com.cn\ "> ";
Pattern p=Pattern.compile(regEx);
Matcher m=p.matcher(str);
String s=m.replaceAll( "\ "site/img\ " "); // ( " ") 删除
System.out.println(s);
}
------解决方案--------------------在js里,我使用反向引用来做,在java,使用反向搜索罢了,效果一样