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

时间:2014-08-05 08:03:31

标签: regex xml 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\-\:]+)[ ]*=[ ]*"[^"]*"


这工作正常,但以下也匹配:

  <string>attribute=b</string>

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

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


我需要使用哪些正则表达式匹配前三个示例?

1 个答案:

答案 0 :(得分:1)

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

添加了一个积极的预测,以检查&gt; x&lt; .Works now。

相关问题