简体   繁体   English

正则表达式以匹配xml元素中的属性名称

[英]Regular expression to match attribute name in xml element

I've the following regular expressions to match: 我有以下正则表达式要匹配:

<string attribute=b>x</string> is matched by : (?<range3>[\w\d\-\:]+)[ ]*=[ ]*[\w\d\-\:]+

<string attribute='b'>x</string> is matched by (?<range1>[\w\d\-\:]+)[ ]*=[ ]*'[^']*'

<string attribute="b">x</string> is matched by (?<range2>[\w\d\-\:]+)[ ]*=[ ]*"[^"]*"


This works fine, however the following is also matched: 这可以正常工作,但是还可以匹配以下内容:

  <string>attribute=b</string>

  <string>attribute='b'</string>

  <string>attribute="b"</string>


What regular expression do I need to use to only match the first three examples? 我需要使用什么正则表达式来匹配前三个示例?

    (?=\S+?>\w+<\S+?)(?<range3>[\w\d\-\:]+)[ ]*=[ ]*[\w\d\-\:]+

添加了正向前瞻以立即检查> x <.Works。

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

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