Java正则表达式匹配XML标签

huangapple go评论63阅读模式
英文:

Java Regex to Match XML tags

问题

我在尝试找出一个正则表达式模式来匹配 XML 标签。

我有两种类型的 XML 标签。
第一种类型

<myTag value="One" value="Two">SomeContentHere</myTag>

这种标签我可以使用以下正则表达式模式进行匹配。

<myTag[\s\S]*?>[\s\S]*?<\/myTag>

第二种类型是,我有同样的标签,显示为 <myTag value="One" value="Two"/>。我在寻找一个正则表达式来匹配这些类型的 XML 标签。我需要像上面第一种类型的匹配那样匹配整个 XML。我的目标是找到一个正则表达式模式,可以捕获上述两种情况。

我尝试了类似 <myTag[\s\S]*?>[\s\S]*?[<\/myTag>]? 这样的模式,但是在这种情况下,这个模式无法捕获我的第一种 XML 标签类型。

请帮助我。

英文:

I am trying a figure out a regex pattern to match XML tags.

I have two kinds of XML tags.
First kind

<myTag value="One" value="Two">SomeContentHere</myTag>

This tag I could match with the following regex pattern.

<myTag[\s\S]*?>[\s\S]*?<\/myTag>

Second kind is, I have the same tag that appear as <myTag value="One" value="Two"/>. I struggle on finding a regex to match these kinds of XML tags. I need to match the entire XML like in the above matching in the first kind. My objective is to find a regex pattern that can capture both the above scenarios.

I tried something like <myTag[\s\S]*?>[\s\S]*?[<\/myTag>]? but, in this case, this pattern fails to capture my first XML tag type

Kindly help me.

答案1

得分: 3

这个社区中有很多关于为什么不应该使用正则表达式来处理这个问题的答案。话虽如此,以下是解决这个问题的方法。如果可能的话,将你的字符串转换为一个文档(Document)。只有当字符串是有效的 XML 时,才能进行转换。然后在文档中查找所需的标签。

代码如下:

private boolean containsTag(String xml, String tagName) {
    Document doc = getDocument(xml);
    if (doc != null) {
        NodeList list = doc.getElementsByTagName(tagName);
        return list != null && list.getLength() > 0;
    }
    return false;
}

private static Document getDocument(String xml) {
    try {
        DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = docBuilder.parse(new InputSource(new StringReader(xml)));
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
英文:

There are tons of answers here in this community on why its bad to use regex for this. Having said that here is the approach for this problem. Convert your string to a Document if it is possible. It is possible if String is a valid xml. Then look for the desired tag in the Document.
Code is :

private boolean containsTag(String xml, String tagName)
    {
        Document doc = getDocument(xml);
        if ( doc != null )
        {
           NodeList list = doc.getElementsByTagName(tagName);
           return list != null && list.getLength() > 0;
        }
        return false;
    }


    private static Document getDocument(String xml) 
    {
         
         try
         {
             DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
             Document doc = docBuilder.parse(new InputSource(new StringReader(xml)));
             return doc;
         } 
         catch (Exception e) 
         {
             e.printStackTrace();
         }
         return null;
   }

答案2

得分: 1

• 对于第一种标记类型,请使用:(<myTag)([\s\S]*?)(>)([\s\S]*?)(<\/myTag>)

• 对于第二种标记类型,请使用:(<myTag)([\s\S]*?)(\/>)

• 对于同时匹配两种类型,请使用:(<myTag)([\s\S]*?)(\/>)|(<myTag)([\s\S]*?)(>)([\s\S]*?)(<\/myTag>)

第一种类型示例

第二种类型示例

同时匹配两种类型示例

英文:

• For your first type of tag use: (<myTag)([\s\S]*?)(>)([\s\S]*?)(<\/myTag>)

• For your second type of tag use: (<myTag)([\s\S]*?)(\/>)

• For both type at the same time use: (<myTag)([\s\S]*?)(\/>)|(<myTag)([\s\S]*?)(>)([\s\S]*?)(<\/myTag>)

FirstTypeExample

SecondTypeExample

BothTypeAtTheSameTimeExample

huangapple
  • 本文由 发表于 2020年4月10日 20:46:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/61140545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定