4

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

mikej
  • 65,295
  • 17
  • 152
  • 131
Lawrence
  • 61
  • 1
  • 3
  • 6
    Don't use REGEX for parsing XML/HTML. Use an XML parser for that. Secondly, could you post your regex that's not working? – ircmaxell Jun 15 '10 at 15:05
  • See related answer: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Francois G Jul 07 '10 at 10:40

1 Answers1

5

/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
  • 43,482
  • 6
  • 101
  • 102
  • I think you need to add a `["']` after your closing parens. – Glen Solsberry Jun 15 '10 at 15:06
  • 1
    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 Jun 15 '10 at 15:08
  • What happens with `` It's perfectly valid XML, but the regex will only capture `something[`. You'd need a backreference to tell the difference... – ircmaxell Jun 15 '10 at 15:15
  • @ircmaxell which is why you're right to say don't use REGEX for parsing XML/HTML – mikej Jun 15 '10 at 15:23
  • 1
    @ircmaxell - since regex is hardly the solution for any xml parsing, I only implement what's required in OP's post. – Matt Jun 15 '10 at 15:23