Java在两个子串之间更改文本

时间:2014-05-15 22:02:57

标签: java string

假设我有这个字符串:

String s = "random text blah blah <change>hello</change> more random text <change>hey</change> ..";

我想更改<change></change>元素之间的值(我想对它们进行编码,或对它们进行解码,在这种情况下它只是一个示例,请注意编码这些值,我需要标签之间的值(在更改之前)本身。)

最好的方法是什么? 我在考虑使用s.replaceAll()函数,但我不确定如何在这个例子中使用它。

我不能只使用XML解析器,因为标签之间的文本可能包含一些特殊字符,例如&lt;和&gt;,这将在使用XML解析器时导致错误。

我正在使用Java。

7 个答案:

答案 0 :(得分:4)

因为您声称这不是有效的XML文档,您可以尝试使用正则表达式。要使用新版本替换已创建的值,您可以使用appendReplacement类中的appendTailMatcher

  • appendReplacement用新版本替换已创建的值。您决定如何替换它。
  • appendTail将最后一场比赛后的部分添加到缓冲区。

要查找<change></change>之间的匹配,您可以使用<change>(.*?)</change>正则表达式 - 如果您希望点代表所有字符(包括\n等行分隔符),您应该使用来自DOTALL的{​​{1}}标记。

演示:

Pattern

输出:

String input = "random text blah blah <change>hello</change> more random text <change>hey</change> ..";
StringBuffer sb = new StringBuffer();

Pattern p = Pattern.compile("<change>(.*?)</change>",Pattern.DOTALL);
Matcher m = p.matcher(input);

while(m.find()){
    String valueFromTags = m.group(1);
    m.appendReplacement(sb, valueFromTags.toUpperCase());
    //                                    ^^^^^^^^^^^^^
    // you decide what to put as replacement of original value
    // toUpperCase is just example
}
m.appendTail(sb);

String result = sb.toString();
System.out.println(result);

答案 1 :(得分:2)

你可以使用正则表达式,但它有点慢。

String newString = s.replaceAll("(?<=<change>).+?(?=<\\/change>)", "Your new string");

这意味着你可以有额外的&lt;或者&gt;更改位中的字符,它仍然可以正常工作。

编辑:如果您还想使用原始字词,可以使用:

    String regexPattern = "(?<=<change>).+?(?=<\\/change>)";
    String originalString = "random text blah blah <change>hello</change> more random text <change>hey</change> ..";

    Pattern pattern = Pattern.compile(regexPattern);
    Matcher matches = pattern.matcher(originalString);

    if (matches.find()){
        String originalText = matches.group(0);
        String t = originalString.replaceAll(regexPattern, originalText + " whatever you want to add");
        System.out.println(t);
    }
    else {
        System.out.println("No matches found");
    }

答案 2 :(得分:0)

您需要使用XML表示法吗?

您也可以使用@change

答案 3 :(得分:0)

这是一种方法:

    String s = "random text blah blah <change>hello</change> more random text <change>hey</change> .."
    String formatted = s.replaceAll("hello", "YOUR CHANGE HERE");
    formatted = s.replaceAll("hey", "YOUR CHANGE HERE");

或者您可以利用正则表达式替换所有:

修改

    String s = "random text blah blah <change>hello</change> more random text <change>hey</change> ..";
    String formatted = s.replaceAll("<change>(\\w)+</change>", "YOUR CHANGE HERE");
    System.out.println(formatted);

答案 4 :(得分:0)

有趣的谜题。

假设您想要更改标签:

public class Test
{
    public static void main(String[] args)
    {
        String s = "random text blah blah <change>hello</change> more random text <change>hey</change> ..";
        System.out.println("BEFORE:"+s);
        System.out.println("AFTER :"+replace(s, "HI", "HELLO"));
    }

    private static String replace(String source, String ...replace)
    {
        if (source == null)
            return null;
        // ... more checks here
        int index=0, next, m=0;
        do
        {
            index = source.indexOf("<change>", index);
            next = source.indexOf("</change>", index)+"</change>".length();
            if (index>0)
            {
                source = source.substring(0, index) + replace[m] + source.substring(next);
                m++;
            }
        }
        while (index>0);
        return source;
    }

}

输出为

BEFORE:random text blah blah <change>hello</change> more random text <change>hey</change> ..
AFTER :random text blah blah HI more random text HELLO ..

答案 5 :(得分:0)

这可能不是一个好主意

寻找发生&lt; &gt; 并替换它们。假设没有广义的“&lt;&gt;”在字符串

String s = "random text blah blah <change>hello</change> more random text <change>hey</change> .."
 String formatted = s.replaceAll("\\>", "><").replaceAll("\\<","/><");

答案 6 :(得分:0)

这是一个适用于正则表达式的解决方案:

    public static void main(String[] args) {
        final String SIMPLE_TAG_REGEX = "<(.+?)>(.+?)</(.+?)>";
        final Pattern PATTERN = Pattern.compile(SIMPLE_TAG_REGEX);

        final String s = "hello <foo>bar</foo> world, <lorem>ipsum</lorem>";
        final Matcher matcher = PATTERN.matcher(s);
        while (matcher.find()) {
            final String startTag = matcher.group(1);
            final String content = matcher.group(2);
            final String endTag = matcher.group(3);
            System.out.println(startTag + ", " + endTag + ": " + content);
        }
    }

打印出来:

    foo, foo: bar
    lorem, lorem: ipsum

请检查startTag.equals(endTag)。正则表达式不能在theorie(und praxis :))中做到这一点!