简体   繁体   English

xml中的正则表达式

[英]regular expression in xml

The Eclipse software has a functionality to search for regular expressions. Eclipse软件具有搜索正则表达式的功能。 I'd like to search for all <version>0.0.1-SNAPSHOT</version> strings whose xml parent node is not <parent> . 我想搜索xml父节点不是<parent>所有<version>0.0.1-SNAPSHOT</version>字符串。

What would be the easiest way to do that ? 最简单的方法是什么?

Edit: Complete rewrite. 编辑:完成重写。

As long as <parent> tags can't be nested, this is possible, but only then (and all the usual caveats about not trying to match XML with regexes apply. As soon as you have comments or CDATA sections in your XML, all bets are off). 只要不能嵌套<parent>标记,就可以做到这一点,但是只有这样(并且所有有关不尝试将XML与正则表达式匹配的常见警告都适用。在XML中包含注释或CDATA部分后,所有赌注关闭)。

(?s)<version>0\.0\.1-SNAPSHOT</version>(?!(?:(?!</?parent>).)*</parent>)

Explanation: 说明:

(?s)              # Turn on singleline matching mode
<version>0\.0\.1-SNAPSHOT</version> # Match this string
(?!               # but only do this if it's impossible to match this regex here:
 (?:              # Try to match...
  (?!</?parent)   #  (as long as there is no <parent> or </parent> tag ahead)
  .               #  any character
 )*               # any number of times
 </parent>        # Then match </parent>
)                 # End of lookahead

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

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