简体   繁体   English

正则表达式XML标签内部带有尖括号

[英]Regex XML tags having angle brackets inside

I need a regex which will give me one XML tag eg <ABC/> or <ABC></ABC> 我需要一个正则表达式,它将给我一个XML标记,例如<ABC/><ABC></ABC>

So, here if I use <(.)+?> , it will give me <ABC> or <ABC> or </ABC> . 因此,在这里,如果我使用<(.)+?> ,它将给我<ABC><ABC></ABC> This is fine. 这可以。

Now, the problem: 现在,问题是:

I have one XML as 我有一个XML作为

<VALUE ABC="10000" PQR="12422700" ADJ="" PROD_TYPE="COCOG EFI LWL P&amp;C >1Y-5Y" SRC="BASE" DATA="data" ACTION="INSERT" ID="100000" GRC_PROD=""/>

Here, if you see, PROD_TYPE="COCOG EFI LWL P&amp;C >1Y-5Y" has a greater than symbol in the value of an attribute. 在这里,如果看到的话, PROD_TYPE="COCOG EFI LWL P&amp;C >1Y-5Y"在属性值中具有大于符号。

So, the regex returns me 所以,正则表达式还给我

<VALUE ABC="10000" PQR="12422700" ADJ="" PROD_TYPE="COCOG EFI LWL P&amp;C >

instead of complete 而不是完整

<VALUE ABC="10000" PQR="12422700" ADJ="" PROD_TYPE="COCOG EFI LWL P&amp;C >1Y-5Y" SRC="BASE" DATA="data" ACTION="INSERT" ID="100000" GRC_PROD=""/>

I need some regex which will not consider the less than and greater than symbols which are part of value ie enclosed in double quotes. 我需要一些正则表达式,它不会考虑小于和大于符号,它们是值的一部分,即用双引号引起来。

You may try this: 您可以尝试以下方法:

(?i)<[a-z][\w:-]+(?: [a-z][\w:-]+="[^"]*")*/?>

And the explanation goes here below: 解释如下:

(?i)         # Match the remainder of the regex with the options: case insensitive (i)
<            # Match the character “<” literally
[a-z]        # Match a single character in the range between “a” and “z”
[\\w:-]       # Match a single character present in the list below
                # A word character (letters, digits, and underscores)
                # The character “:”
                # The character “-”
   +            # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
(?:          # Match the regular expression below
   \\            # Match the character “ ” literally
   [a-z]        # Match a single character in the range between “a” and “z”
   [\\w:-]       # Match a single character present in the list below
                   # A word character (letters, digits, and underscores)
                   # The character “:”
                   # The character “-”
      +            # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   =\"           # Match the characters “=\"” literally
   [^\"]         # Match any character that is NOT a “\"”
      *            # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   \"            # Match the character “\"” literally
)*           # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
/            # Match the character “/” literally
   ?            # Between zero and one times, as many times as possible, giving back as needed (greedy)
>            # Match the character “>” literally

And if you like like to include open , close or self-closed tags then try below RegEx : 而且,如果您想包含opencloseself-closed标记,请尝试使用RegEx

(?i)(?:<([a-z][\w:-]+)(?: [a-z][\w:-]+="[^"]*")*>.+?</\1>|<([a-z][\w:-]+)(?: [a-z][\w:-]+="[^"]*")*/>)

A java code frag implementing the same: 实现相同的java代码片段:

try {
    boolean foundMatch = subjectString.matches("(?i)(?:<([a-z][\\w:-]+)(?: [a-z][\\w:-]+=\"[^\"]*\")*>.+?</\\1>|<([a-z][\\w:-]+)(?: [a-z][\\w:-]+=\"[^\"]*\")*/>)");
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}

Hope this helps... 希望这可以帮助...

To expand on the point of G_H's link: Don't use regex to parse XML. 为了扩展G_H的链接,请执行以下操作: 不要使用正则表达式来解析XML。 Use XPath to return a Node, and pass that Node to an identity Transformer : 使用XPath返回一个Node,并将该Node传递给标识Transformer

Node valueElement = (Node)
    XPathFactory.newInstance().newXPath().evaluate("//VALUE",
        new InputSource(new StringReader(xmlDocument)),
        XPathConstants.NODE);

StringWriter result = new StringWriter();
TransformerFactory.newInstance().newTransformer().transform(
    new DOMSource(valueElement), new StreamResult(result));

String valueElementMarkup = result.toString();

Also try this: 也尝试一下:

<.*?(".*?".*?)*?>

It grabs everything between < and > only if even number of " double quotes are present. Pairs of double quotes mean that stuff is enclosed in. Otherwise it skips > symbol and keep searching further for the next one > (which should be happen after closing " quote) 它抓住之间的一切<>仅在偶数"的意思是双引号对双引号是存在的。这些东西被封闭在,否则跳过>符号,并保持下一个进一步搜索> (这应该是收盘后发生"报价)

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

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