tags:

views:

107

answers:

2

Hello!

I need to extract from the following lines of code

<label for="<%=foobar.bar %>">Password:</label>

<label for="foobar">Password:</label>

I need to extract foobar, I can use this: (?<=for=")[^"]+(?=(")) to extract:

<%=foobar.bar %>

and

foobar

but I don't want <%= or .bar and if I try to create (?<=for=")[^"]+(?=(")) | (?<=for="<%=)[^"]+(?=(")) it doesn't work becuase the label that included <%= meets both conditions and I don't think you can use XOR? Is this anything anyone can help me with?

Merci :)

A: 

You can try this:

(?<for="(<%=)?)[^" ]*(?=( %>)?")

Assuming that what you want to capture never includes spaces. Otherwise you can try:

(?<for="(<%=)?)[^"]*?(?=( %>)?")

To use a non greedy form of [^"]*

Nathan Fellman
+1  A: 

I believe that it's better to not create uber-regexes. Do your task in several steps:

  1. Extract <%=foobar.bar %> or foobar with your regex (?<=for=")[^"]+(?=("))
  2. Check if result matches regex like <%=([\w]+)\.bar\s*%>.
  3. If it does use $1 group from match, otherwise use result of step 2.
  4. You get foobar
Rorick
thank you, I used (?<=for=\")[^\"]+(?=(\")) and then went on to use: (?<=<%=\s*)[^\s]+(?=\.bar\s*%>)
Sara