日期:2014-05-20  浏览次数:20709 次

Java简单正则表达式问题
正则表达式学了忘忘了学太痛苦了,请csdn每天用比较熟悉的同学帮帮忙。

要求:给定html字符串,将图片链接中的文件名中空格去掉

原字符串例子:String s1 = 'ttached muscles. </P><P align=center><IMG height=286 alt="eye_anatomy 1.jpg" src="file:///android_asset/images/eye_ anatomy 1.jpg" width=350 bord'

去除后字符串:String s2 = 'ttached muscles. </P><P align=center><IMG height=286 alt="eye_anatomy 1.jpg" src="file:///android_asset/images/eye_anatomy1.jpg" width=350 bord'

可以确定的是'src="file:///android_asset/images/'肯定存在, 图片链接肯帝有扩展名,有可能是.bmp,.gif,.jpg等等,字符串中可能有若干图片链接,

分不够我可以回帖补.有什么问题可以留言

谢谢

------解决方案--------------------
Java code

String str = "\r\n<P align=center>\r\n"
                + "<IMG height=286 alt=\"eye_anatomy 1.jpg\" src=\"file:///android_asset/images/eye_anatomy 1  .jpg\" width=350 border=0></P> fd ssda f \r\n"
                + "fd  <P align=center><IMG height=286 alt=\"eye_anatomy 1.jpg\" src=\"file:///android_asset/images/  eye_anato  my 1 .bmp\" width=350 border=0></P>\r\n"
                + "fd  <P align=center><IMG height=286 alt=\"eye_anatomy 1.jpg\" src=\"file:///android_asset/images/  eye_anato  my 1 .bmp\" width=350 border=0></P>\r\n"
                + "fd  <P align=center><IMG height=286 alt=\"eye_anatomy 1.jpg\" src=\"file:///android_asset/images/  eye_anato  my 1 .bmp\" width=350 border=0></P>\r\n"
                + "fd  <P align=center><IMG height=286 alt=\"eye_anatomy 1.jpg\" src=\"file:///android_asset/images/  eye_anato  my 1 .bmp\" width=350 border=0></P>\r\n"
                + "\r\n<IMG height=286 alt=\"eye_anatomy 1.jpg\" src=\"file:///android_asset/images/eye_anatomy1.png\" width=350 border=0>\r\n"
                + "\r\n<IMG height=286 alt=\"eye_anatomy 1.jpg\" src=\"file:///android_asset/images/eye_anatomy1.png\" width=350 border=0>\r\n"
                + "\r\n<IMG height=286 alt=\"eye_anatomy 1.jpg\" src=\"file:///android_asset/images/eye_anatomy1.png\" width=350 border=0>\r\n"
                + "\r\n<IMG height=286 alt=\"eye_anatomy 1.jpg\" src=\"file:///android_asset/images/eye_anatomy1.png\" width=350 border=0>\r\n"
                + "\r\n<IMG height=286 alt=\"eye_anatomy 1.jpg\" src=\"file:///android_asset/images/eye_anatomy1.png\" width=350 border=0>\r\n";

Pattern p = Pattern.compile(
                "(.*?)(src=\"file:///android_asset/images/)(.*?)(\")",
                Pattern.DOTALL);
// nanoseconds 2600356

Pattern p = Pattern.compile(
                "(.*?)(src=\"file:///android_asset/images/)(.*?\\s+.*?)(\")",
                Pattern.DOTALL);
// nanoseconds 6596175