0

拥有这样的 XML:

<Abc>
    <Bcd>C</Bcd>
    <Def>E</Def>
    <Fgh>G</Fgh>
</Abc>

哪个是正确的 Java RegEx 将其更改为以下内容?

-Abc-
    -Bcd-Cde-/Bcd-
    <Def>E</Def>
    <Fgh>G</Fgh>
-/Abc-

我做到了:

myString.replaceAll("\\<(.*?)\\>","-$1-");

但我无法进行正确的排除,因此只更改了一些标签。

更新:使用对 Pshemo 答案的否定就可以了:

myString.replaceAll("\\<?!(KWD|COM|PLN|PUN|TYP|STR)\\>","&lt;$1&gt;")
4

2 回答 2

3

尝试

"\\<[AB]\\>"

为您的正则表达式。它将找到 < 然后 A 或 B 然后 >。有关更多信息,请参阅http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

要进一步扩展它以获得完整的字符集,您可以使用以下格式。

public class RegexString
{
    private static String myString = "<Abc>\n" 
            + "    <Bcd>C</Bcd>\n"
            + "    <Def>E</Def>\n" 
            + "    <Fgh>G</Fgh>\n" 
            + "</Abc>";
    private static final String HTML_TAGS = "/?(Abc|Bcd)";
    private static final String FULL_REGEX = "<" + HTML_TAGS + ">";

    public static void main(String[] args)
    {
          System.out.println(myString.replaceAll(FULL_REGEX, "-$1-"));
    }
}
于 2013-09-12T15:53:49.327 回答
1

Using regex on HTML code is not good idea as pointed in this question but if you really want to complicate your life then you can use OR | operator like

String yourData = "<Abc>\n" 
                + "    <Bcd>C</Bcd>\n"
                + "    <Def>E</Def>\n" 
                + "    <Fgh>G</Fgh>\n" 
                + "</Abc>";
System.out.println(yourData.replaceAll("</?(Abc|Bcd)>", "-$1-"));

output:

-Abc-
    -Bcd-C-Bcd-
    <Def>E</Def>
    <Fgh>G</Fgh>
-Abc-
于 2013-09-12T16:04:22.310 回答