我的测试用文件this is test-text content:
1.<img src="../a.gif">
2.<img src="a.gif">
3.<link href="../a.gif">
4.<link href="a.gif">
5.<link href="http://www.a.b/c.gif">
6.<a href="#a.htm">
5.<a href="http://www.a.b/c.gif">
7.<a href="mailto:[email protected]">谢谢各位了

解决方案 »

  1.   

    表达式
    regex = "[\\w\\W]*(\\s*[h|H][R|r][E|e][F|f]\\s*=\\s*\"\\s*\\.\\s*#\\s*[(http)(mailto)])[\\w|\\W]*"
    可以屏蔽掉:.#http开头的和.#mailto开头的如果也要屏蔽掉 ".#http mailto的话表达式如下:
    regex1 = "[\\w\\W]*(\\s*[h|H][R|r][E|e][F|f]\\s*=\\s*\"\\s*\\.\\s*#\\s*[(http)(mailto)((http)\\s*(mailto))])[\\w|\\W]*";
    不知道能否完成你要的功能.
      

  2.   

    import java.util.regex.Pattern;
    import java.util.regex.Matcher;public class href {
        public static void main(String[] args) {
            Pattern p = Pattern.compile(".+href=\"(?!(\\.|#|http:|mailto:)).+",
                    Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher("");
            String[] example = {
                "<img src=\"../a.gif\">",
                "<img src=\"a.gif\">",
                "<link href=\"../a.gif\">",
                "<link href=\"a.gif\">",
                "<link href=\"http://www.a.b/c.gif\">",
                "<a href=\"#a.htm\">",
                "<a href=\"http://www.a.b/c.gif\">",
                "<a href=\"mailto:[email protected]\">", };        for (int i = 0; i < example.length; i++) {
                m.reset(example[i]);
                if (m.matches()) {
                    System.out.println(m.group());
                }
            }
        }
    }如此即可
      

  3.   

    to:jackkui(键盘上的武士) 
    我可能让你误会了,我是指屏蔽以 . # http mailto 中任一个开头的href,不是这些一起开头;你看我的test用例就知道是什么意思了。to:registered(已注册) 
    我不能使用?!,因为它匹配 pattern 但不获取匹配结果,我需要得到获取匹配结果;另外,我也不能使用诸如if (m.matches()) 的判断语句,因为我使用replaceAll()做文档替换。好像没有判断后做单一替换的方法吧(只有replaceFirst和replaceAll),你觉得怎么解决?
      

  4.   

    我的测试用文件
    this is test-text content:
    1.<img src="../a.gif">
    2.<img src="a.gif">
    3.<link href="../a.gif">
    4.<link href="a.gif">
    5.<link href="http://www.a.b/c.gif">
    6.<a href="#a.htm">
    7.<a href="http://www.a.b/c.gif">
    8.<a href="mailto:[email protected]">
    我想替换第4条的为全路径d:/test/a.gif,需求就是:
    有一个文件流,我要把里面的相对路径全部替换为绝对路径,当然,../ http mailto 等不能替换(../在后一步操作即可)
      

  5.   

    需要使用两个 Pattern
    一个用来过滤, 另一个用来替换
    而且批量处理不要用 String 的 replace, 开销太大public class href {
        public static void main(String[] args) {
            Pattern p = Pattern.compile(".+href=\"(?!(\\.|#|http:|mailto:)).+",
                    Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher("");
            Pattern p2 = Pattern.compile("\".+\"");
            Matcher m2 = p2.matcher("");
            StringBuffer buff;        String[] example = {
                "<img src=\"../a.gif\">",
                "<img src=\"a.gif\">",
                "<link href=\"../a.gif\">",
                "<link href=\"a.gif\">",
                "<link href=\"http://www.a.b/c.gif\">",
                "<a href=\"#a.htm\">",
                "<a href=\"http://www.a.b/c.gif\">",
                "<a href=\"mailto:[email protected]\">", };
            String replacement = "\"d:/test/a.gif\"";        for (int i = 0; i < example.length; i++) {
                m.reset(example[i]);
                m2.reset(example[i]);
                if (m.matches()) {
                    buff = new StringBuffer();
                    while (m2.find()) {
                        m2.appendReplacement(buff, replacement);
                    }
                    m2.appendTail(buff);
                    System.out.println(buff.toString());
                }
            }
        }
    }
      

  6.   

    你提供了不少思想,让你失望的是,我还是不能根据你的思路和代码改出我的需求,
    首先,我有些东西没说明白:
    1 我的test内容只是为方便给大家帮我测试,才写的很工整,我的输入实际中是任意文件输入流,即是html文件流。所以不能是工整的数组循环,也因此,应该replacement=d:/test+文件名;
    2 由于上,我觉得判断语句if (m.matches()) while (m2.find()) 我都用不上,因为我用replaceAll(),如果不用replaceAll(),我就不知道用什么了,
    3 ?!并不返回匹配,而我的文件名是必须靠group(n)来动态获得的;
    你说呢?
      

  7.   

    这是我目前的相关代码,只实现了 “屏蔽 . # http ”正确,其完整的测试结果可看顶楼的阐述,谢谢谢谢,呵呵
        public static void call()
        {
            try
            {
                String sUrl = "";
                File file = new File("test.txt");
                URL url = file.toURL();
                StringBuffer sb = new StringBuffer();
                InputStream in = url.openStream();
                InputStream input = new BufferedInputStream(in);
                Reader reader = new InputStreamReader(input);
                for (int c = reader.read(); c >= 0; c = reader.read())
                {
                    sb.append((char) c);
                }
                String sInput = sb.toString();            System.out.println(sInput);            String sPattern = "";            String srcPath = "D:/Test/";
                sPattern = "\\s[Hh][Rr][Ee][Ff]\\s*=\\s*\"([^\\.#(http)]+[^\"]*)\"";            String sOutput = matchFilePath(sInput, sPattern, srcPath, "href");            System.out.println(sOutput);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        
        public static String matchFilePath(String sInput, String sPattern,
                String replacement, String attrName)
        {
            String outer = null;        try
            {
                Pattern pattern = Pattern.compile(sPattern,
                        Pattern.CASE_INSENSITIVE);
                Matcher matcher = pattern.matcher(sInput);            //System.out.println(matcher.groupCount());            while (matcher.find())
                {
                    String sMatch = matcher.group(1);
                    System.out.println("sMatch = " + sMatch);
                }            outer = matcher.replaceAll(" " + attrName + "=\"" + replacement
                        + "$1\"");        }        catch (PatternSyntaxException e)
            {
                e.printStackTrace();            
            }        return outer;
        }
      

  8.   

    import java.util.regex.Pattern;
    import java.util.regex.Matcher;public class ReplaceHref {
        public static String replaceHref(String html, String srcPath) {
            Pattern p1 = Pattern.compile(
                    "<.*href=\"(?!(\\.|#|http:|mailto:)).*\">",
                    Pattern.CASE_INSENSITIVE);
            Pattern p2 = Pattern.compile("\"(.*)\"");
            Matcher m1 = p1.matcher(html);
            Matcher m2 = p2.matcher("");
            StringBuffer buff = new StringBuffer();
            String blank = "";        while (m1.find()) {
                m1.appendReplacement(buff, blank);
                m2.reset(m1.group(0));
                while (m2.find()) {
                    m2.appendReplacement(buff,
                            "\"" + srcPath + "$1" + "\"");
                }
                m2.appendTail(buff);
            }
            m1.appendTail(buff);        return buff.toString();
        }    public static void main(String[] args) {
            String example = "1.<img src=\"../a.gif\">\n" +
                    "2.<img src=\"a.gif\">\n" +
                    "3.<link href=\"../a.gif\">\n" +
                    "4.<link href=\"a.gif\">\n" +
                    "5.<link href=\"http://www.a.b/c.gif\">\n" +
                    "6.<a href=\"#a.htm\">\n" +
                    "5.<a href=\"http://www.a.b/c.gif\">\n" +
                    "7.<a href=\"mailto:[email protected]\">";
            String path = "C:\\\\Windows\\\\";        System.out.println(replaceHref(example, path));
        }
    }
      

  9.   

    非常感谢你在百忙之中为我接刀,我已按你的思路修改完成,谢谢!
    我觉得你的Pattern2/matcher2设置的真是妙啊!呵呵
    (不过,regex表达式有点小问题,我在正式的测试中居然可以匹配align="right",然后该匹配的没匹配,我做了改动就好了,当然还是用你的推崇的“?!”)
      

  10.   

    msn: [email protected], 还请赐教!