简体   繁体   English

Java Regex检查字符串是否包含XML标记

[英]Java Regex check if string contains XML tag

I am trying to determine if a string contains at least one XML tag using the String.match() function. 我试图使用String.match()函数确定字符串是否包含至少一个XML标记。 Due to the way the project is set up, I would prefer if I didn't have to use a Pattern . 由于项目的设置方式,我更愿意,如果我不必使用Pattern

Currently I use this Regex: 目前我使用这个正则表达式:

<[A-Za-z0-9]+>

Which obviously only checks if the string has right and left arrow brackets that contains text. 这显然只检查字符串是否包含包含文本的右箭头括号和左箭头括号。 What I need is a way to check if the string just has a single XML tag with Regex, eg input like: 我需要的是一种方法来检查字符串是否只有一个带有Regex的XML标签,例如输入如下:

blah <abc foo="bar">blah</abc> blah
blah <abc foo="bar"/>

but not input like: 但不输入如下:

blah <abc> blah
blah <abc </abc> blah

Is that possible? 那可能吗?

This: 这个:

if (input.matches("(?s).*(<(\\w+)[^>]*>.*</\\2>|<(\\w+)[^>]*/>).*"))

matches both types of tag (standard and self-closing): 匹配两种类型的标签(标准和自动关闭):

<abc foo="bar">blah</abc>
<abc foo="bar"/>

without matching incomplete tags like: 不匹配不完整的标签,如:

<abc>

See regex live demo . 请参阅regex现场演示

You can use: 您可以使用:

if (input.matches("(?s).*?<(\\S+?)[^>]*>.*?</\\1>.*")) {
    // String has a XML tag
}

(?s) is DOTALL flag to make DOT match newlines also. (?s)DOTALL标志,也可以使DOT匹配换行符。

RegEx Demo RegEx演示

Ok, this regex will match most html/xml tags. 好的,这个正则表达式将匹配大多数html / xml标签。
Probably only need node tags, the rest can be peeled off. 可能只需要节点标签,其余的可以剥离。

Just node tags (final edit) - 只是节点标签(最终编辑) -

 # "(?s)<(?:/?[\\w:]+\\s*|[\\w:]+(?:\".*?\"|'.*?'|[^>]*?)+)>"

 (?s)
 <
 (?:
      /?
      [\w:]+ 
      \s* 
   |  
      [\w:]+ 
      (?: " .*? " | ' .*? ' | [^>]*? )+
 )
 >

Full - 全 -

Formatted: 格式:

 # "<(?:(?:/?[\\w:]+\\s*/?)|(?:[\\w:]+\\s+(?:(?:(?:\"[\\S\\s]*?\")|(?:'[\\S\\s]*?'))|(?:[^>]*?))+\\s*/?)|\\?[\\S\\s]*?\\?|(?:!(?:(?:DOCTYPE[\\S\\s]*?)|(?:\\[CDATA\\[[\\S\\s]*?\\]\\])|(?:--[\\S\\s]*?--)|(?:ATTLIST[\\S\\s]*?)|(?:ENTITY[\\S\\s]*?)|(?:ELEMENT[\\S\\s]*?))))>"

 <
 (?:
      (?:
           /? 
           [\w:]+ 
           \s* 
           /? 
      )
   |  
      (?:
           [\w:]+ 
           \s+ 
           (?:
                (?:
                     (?: " [\S\s]*? " )
                  |  (?: ' [\S\s]*? ' )
                )
             |  (?: [^>]*? )
           )+
           \s* 
           /? 
      )
   |  
      \?
      [\S\s]*? 
      \?
   |  
      (?:
           !
           (?:
                (?:
                     DOCTYPE
                     [\S\s]*? 
                )
             |  (?:
                     \[CDATA\[
                     [\S\s]*? 
                     \]\]
                )
             |  (?:
                     --
                     [\S\s]*? 
                     --
                )
             |  (?:
                     ATTLIST
                     [\S\s]*? 
                )
             |  (?:
                     ENTITY
                     [\S\s]*? 
                )
             |  (?:
                     ELEMENT
                     [\S\s]*? 
                )
           )
      )
 )
 >

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

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