tags:

views:

48

answers:

1

Hi

What would the regular expression be to return 'details.jsp' (without quotes!) from this original tag. I can quite easily match all of value="details.jsp" but am having trouble just matching the contents within the attribute.

<s:include value="details.jsp" />

Any help greatly appreciated!

Thanks

Lawrence

+1  A: 

/value=["']([^'"]+)/ would place "details.jsp" in the first capture group.

Edit:

In response to ircmaxell's comment, if you really need it, the following expression is more flexible:

/value=(['"])(.+)\1/

It will match things like <s:include value="something['else']">, but just note that the value will be placed in the second capture group.

But as mentioned before, regex is not what you want to use for parsing XML (unless it's a really simple case), so don't invest too much time into complex regexes when you should be using a parser.

Matt
I think you need to add a `["']` after your closing parens.
gms8994
Whilst it wouldn't hurt to add it the `["']` isn't needed because `([^'"]+)` matches (and captures) all characters upto a `'` or `"` and then stops so it will match upto but not including the closing quote.
mikej
What happens with `<s:include value="something['else']">` It's perfectly valid XML, but the regex will only capture `something[`. You'd need a backreference to tell the difference...
ircmaxell
@ircmaxell which is why you're right to say don't use REGEX for parsing XML/HTML
mikej
@ircmaxell - since regex is hardly the solution for any xml parsing, I only implement what's required in OP's post.
Matt