Stef Heyenrath
Stef Heyenrath

Reputation: 9830

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?

Upvotes: 0

Views: 1687

Answers (1)

vks
vks

Reputation: 67968

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

Added a positive lookahead to check for >x<.Works now.

Upvotes: 1

Related Questions