1

I'm trying to find all labels with an id and text, but my regex doesn't seem to work:

With the following regex:

<asp:[a-z]+.*? ID="(?<id>.*?)".*? Text="(?<text>.*?)".*?/>

and the following sample text:

<asp:Label ID="SomeID" Text="SomeText" />
<asp:Label Text="SomeText" />
<asp:Label ID="SomeID" />
<asp:Label ID="SomeOtherID" Text="Some Other Text" />

I get the following matches:

   1. "<asp:Label ID="SomeID" Text="SomeText" />" has 2 groups:
         1. "SomeID"
         2. "SomeText"
   2. "<asp:Label Text="SomeText" /> <asp:Label ID="SomeID" /> <asp:Label ID="SomeOtherID" Text="Some Other Text" />" has 2 groups:
         1. "SomeID"
         2. "Some Other Text"

The first one is obviously correct, but I'm not sure why #2 shows up.

And the following regex only finds the first label ("SomeID") but not the fourth one ("SomeOtherID"):

<asp:[a-z]+ (?!.*<[a-z]).*? ID="(?<id>.*?)".*? Text="(?<text>.*?)".*?/>
Sadhana
  • 135
  • 1
  • 8
  • 2
    There's a well-worded explanation here! http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Andomar Nov 17 '09 at 15:57
  • Why are you searching your code within your app? Surely there is a better way of doing this? – Byron Whitlock Nov 17 '09 at 15:57
  • Maybe he's not searching his code within his app but rather within his editor? There are IDEs with regex support around... – Heinzi Nov 17 '09 at 16:04
  • I'm trying to find a list of all the phrases that need to be translated. – Sadhana Nov 17 '09 at 16:40

1 Answers1

2

Try replacing the .*s in your expression with [^>]*, to avoid crossing HTML tag boundaries. The problem is that the .*? in the middle of your expression matches /> <asp:Label ID="SomeOtherID".

Perhaps something like this:

<asp:[a-z]+\s*ID="(?<id>[^"]*)"\s*Text="(?<text>[^"]*)"[^/]*/>
Jeremy Stein
  • 19,171
  • 16
  • 68
  • 83
Heinzi
  • 167,459
  • 57
  • 363
  • 519