Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java正则表达式-如何替换模式或如何_Java_Regex_Replace - Fatal编程技术网

Java正则表达式-如何替换模式或如何

Java正则表达式-如何替换模式或如何,java,regex,replace,Java,Regex,Replace,我有一堆HTML文件。在这些文件中,我需要更正IMG标签的src属性。 IMG标签通常如下所示: <img alt="" src="./Suitbert_files/233px-Suitbertus.jpg" class="thumbimage" height="243" width="233" />` <img alt="" src="Suitbert%20%E2%80%93%20Wikipedia_files/233px-Suitbertus.jpg" class="th

我有一堆HTML文件。在这些文件中,我需要更正IMG标签的
src
属性。 IMG标签通常如下所示:

<img alt="" src="./Suitbert_files/233px-Suitbertus.jpg" class="thumbimage" height="243" width="233" />`
<img alt="" src="Suitbert%20%E2%80%93%20Wikipedia_files/233px-Suitbertus.jpg" class="thumbimage" height="243" width="233" />
<img src="good" /><img src="./bad" />
到目前为止,我有以下课程:

import java.util.regex.*;


public class Replacer {

    // this PATTERN should find all img tags with 0 or more attributes before the src-attribute
    private static final String PATTERN = "<img\\.*\\ssrc=\"\\./";
    private static final String REPLACEMENT = "<img\\.*\\ssrc=\"";
    private static final Pattern COMPILED_PATTERN = Pattern.compile(PATTERN,  Pattern.CASE_INSENSITIVE);


    public static void findMatches(String html){
        Matcher matcher = COMPILED_PATTERN.matcher(html);
        // Check all occurance
        System.out.println("------------------------");
        System.out.println("Following Matches found:");
        while (matcher.find()) {
            System.out.print("Start index: " + matcher.start());
            System.out.print(" End index: " + matcher.end() + " ");
            System.out.println(matcher.group());
        }
        System.out.println("------------------------");
    }

    public static String replaceMatches(String html){
        //Pattern replace = Pattern.compile("\\s+");
        Matcher matcher = COMPILED_PATTERN.matcher(html);
        html = matcher.replaceAll(REPLACEMENT);
        return html;
    }
}
import java.util.regex.*;
公共类替换程序{
//此模式应该在src属性之前找到所有具有0或更多属性的img标记
私有静态最终字符串模式=”
因此,我的方法
findMatches(stringhtml)
似乎可以正确地找到
src
属性以
/
开头的所有IMG标记

现在,我的方法
replaceMatches(字符串html)
无法正确替换匹配项。 我是regex的新手,但我假设替换regex不正确,或者使用replaceAll方法,或者两者都不正确。 A您可以看到,替换字符串包含两个部分,在所有IMG标记中都是相同的:
。在这两部分之间,应该有原始字符串中的0个或更多HTML属性。
如何构造这样一个替换字符串?

有人能告诉我吗?

不要将正则表达式用于HTML。使用a,获取src属性并替换它。

您的替换不正确。它将替换匹配的字符串(不解释为regexp)。如果要实现所需,需要使用组。组由regexp的括号分隔。每个左括号表示一个新组。 您可以在替换字符串中使用$i来复制组匹配的内容,其中'i'是您的组号参考。有关详细信息,请参阅
appendReplacement
的文档

// Here is an example (it looks a bit like your case but not exactly)
String input = "<img name=\"foobar\" src=\"img.png\">";
String regexp = "<img(.+)src=\"[^\"]+\"(.*)>";
Matcher m = Pattern.compile(regexp).matcher(input);
StringBuffer sb = new StringBuffer();
while(m.find()) {
    // Found a match!
    // Append all chars before the match and then replaces the match by the 
    // replacement (the replacement refers to group 1 & 2 with $1 & $2
    // which match respectively everything between '<img' and 'src' and,
    // everything after the src value and the closing >
    m.appendReplacement(sb, "<img$1src=\"something else\"$2>";
}
m.appendTail(sb);// No more match, we append the end of input
//下面是一个示例(看起来有点像您的案例,但不完全一样)
字符串输入=”;
字符串regexp=“”;
Matcher m=Pattern.compile(regexp).Matcher(input);
StringBuffer sb=新的StringBuffer();
while(m.find()){
//找到一根火柴!
//在匹配之前附加所有字符,然后用
//替换(替换指第1组和第2组的1美元和2美元
//它们分别匹配
m、 (sb)“;
}
m、 appendTail(sb);//不再匹配,我们追加输入的结尾

希望这对您有所帮助

如果
src
属性只出现在您的HTML中
img
标记中,您可以这样做:

input.replace("src=\"./", "src=\"")
如果您使用的是*nix操作系统,您也可以在没有java的情况下使用
sed
,尝试以下方法:

PATTERN = "(<img[^>]*\\ssrc=\")\\./"
REPLACEMENT = "$1"
…您的正则表达式将与此匹配:

<img src="good" /><img src="./


即使您使用了非贪婪的
*?
[^>]*
也可以确保匹配始终包含在一个标记中。

是否调用
替换匹配()
method?为什么不使用javascript执行此操作?迭代img集合,然后从每个.src的开头删除./将非常简单,如果它存在的话。为什么要在Java中执行此操作,而不使用(比如)sed或跨文件搜索/替换的IDE/编辑器?这是适合此任务的工具,而这不是在Java中这样做是有意义的。@TravisJ因为在JavaScript中这样做是在解决问题,而不是解决问题。@Dave:如果我在Eclipse中这样做,我仍然需要知道正确的替换正则表达式。@ggreiner:是的,我知道,来自不同的类,如Replacer.replacesMatches(html)我应该补充一点:当我检查html输出文件时,被替换的标签看起来是这样的:正如你所看到的那样,完全混乱了,所以会发生替换mt,但不正确。如果你只是在搜索一个非常具体的东西,并且它是非常受控制的,那么正则表达式就可以了。在这种情况下,这将是我尝试的第一件事。也就是说,我已经有了基于目录的类似XML的搜索/替换工具,因此,如果它不能立即成功,我会使用这些工具。我已经有了这个想法,但没有保证src属性只出现在IMG标记中。特别是,src属性对很多HTML标记有效,因此这是一种非常不可预测的方法。我已经怀疑了这一点,并且看了appendReplacement。但我对如何做感到困惑。任何指向示例或教程的链接都会很有帮助。没有必要在这里求助于
appendReplacement()
appendTail()
(尽管了解它们当然很好)。
replaceAll()
完全有能力处理这项工作,正如我在回答中所展示的。是的,我只是为前面的评论提供了一个例子。@GuillaumePolet:Thx,你和Alan在上面的帖子确实启发了我并解决了问题。非常有趣,正是我所期待的。Thx,这就是关键。最后我明白了这一点带有$符号的thingie及其在替换字符串中的使用是有效的。这是最终解决方案,Alan More和Guillaume Polet对此表示敬意:`private static final string PATTERN=“(]*\\ssrc=\”)\\./“private static final string REPLACEMENT=“$1”`