简体   繁体   English

使用Java Pattern类对某些字符和数字进行正则表达式

[英]Regular expression for some character and number using Java Pattern class

I have some XML in a String and want to replace all id attribute values by "" where the id attribute value starts with "widget". 我在String中有一些XML,并希望将所有id属性值替换为“”,其中id属性值以“ widget”开头。

Here is my input xml: 这是我的输入xml:

<panel height="24" id="sd1" layout="vertical" left_padding="12" spacing="12" stereotype="directions" width="790" x="0" y="79">
<text_widget halign="left" id="widget5019721000" width="761"/>
<text_widget halign="left" id="widget5019501000" width="761"/>
<text_widget halign="left" id="widget5019711000" width="761"/>
<text_widget halign="left" id="widget5019752000" width="761"/>
</panel>

And the output xml should be like below: 输出的xml应该如下所示:

<panel height="24" id="sd1" layout="vertical" left_padding="12" spacing="12" stereotype="directions" width="790" x="0" y="79">
<text_widget halign="left" id="" width="761"/>
<text_widget halign="left" id="" width="761"/>
<text_widget halign="left" id="" width="761"/>
<text_widget halign="left" id="" width="761"/>
</panel>

I was trying using the code below: 我正在尝试使用以下代码:

String xmlStr = this.getXmlString();
Pattern pattern = Pattern.compile(xmlStr);
Matcher matcher = pattern.matcher(\"id="widget[0-9]\");

How do I do this using a regular expression in Java? 如何使用Java中的正则表达式执行此操作?

There's the error in your code though, Pattern.compile receive String parameter which is regex pattern, not source string. 您的代码中有错误, Pattern.compile接收String参数,这是正则表达式模式,而不是源字符串。 Here's the right code. 这是正确的代码。

Pattern pattern = Pattern.compile("id=\\\"widget\d*")
Matcher match = pattern.matcher(xmlStr)
String result = match.replaceAll("id=\\\"")

String result = xml.replaceAll("id=\\\\\\"widget[0-9]*", "id=\\"");

Instead you can also use XML processing. 相反,您也可以使用XML处理。 Download JDOM from www.jdom.org and add the contained jdom-2.0.4.jar to your classpath. www.jdom.org下载JDOM并将包含的jdom-2.0.4.jar添加到您的类路径中。 Then use the following code: 然后使用以下代码:

import java.io.StringReader;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class Test {
public static void main(String[] args) throws Exception {
    String xml = "<panel height=\"24\" id=\"sd1\" layout=\"vertical\" left_padding=\"12\" spacing=\"12\" stereotype=\"directions\" width=\"790\" x=\"0\" y=\"79\">"
                +"<text_widget halign=\"left\" id=\"widget5019721000\" width=\"761\"/>"
                +"<text_widget halign=\"left\" id=\"widget5019501000\" width=\"761\"/>"
                +"<text_widget halign=\"left\" id=\"widget5019711000\" width=\"761\"/>"
                +"<text_widget halign=\"left\" id=\"widget5019752000\" width=\"761\"/>"
                +"</panel>";

    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build(new StringReader(xml));

    List<Element> textWidgetElements = doc.getRootElement().getChildren("text_widget");
    for (Element e : textWidgetElements) {
        if (e.getAttributeValue("id").startsWith("widget")) {
            e.setAttribute("id", "");
        }
    }

    XMLOutputter out = new XMLOutputter();
    out.setFormat(Format.getPrettyFormat());
    String result = out.outputString(doc);

    System.out.println(result);
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM