0

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?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Stef Heyenrath
  • 9,335
  • 12
  • 66
  • 121
  • 1
    See this: http://stackoverflow.com/a/1732454/418066 – Biffen Aug 05 '14 at 08:09
  • Thanks for the link to that article. (But why a down-vote ?) – Stef Heyenrath Aug 05 '14 at 08:16
  • 1
    I didn't down-vote. But I can see why one would from the lack of effort shown in your question. If you *do* want to use regex, read up on [greediness](http://www.regular-expressions.info/repeat.html). – Biffen Aug 05 '14 at 08:31

1 Answers1

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

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

vks
  • 67,027
  • 10
  • 91
  • 124