-1

I'm using this regex code to get the tags but not the value:

(<input type="hidden" name="pt" id="pt" value=")|(" \/>)

From this code:

<input type="hidden" name="pt" id="pt" value="f64b1aadf7baa6e416dbfb6bf95fa031" />

But how would I do it the other way around? Get the value, but not the surrounding tags? So I would only get "f64b1aadf7baa6e416dbfb6bf95fa031" (without the quotes). Thanks.

Joey Morani
  • 25,431
  • 32
  • 84
  • 131
  • 5
    [Don't use regex to parse HTML, etc.](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Donut Oct 11 '11 at 16:39
  • What language? All use regexes a bit differently. – Chriszuma Oct 11 '11 at 16:41
  • Try `value = "f64b1aadf7baa6e416dbfb6bf95fa031"`. You have to be more specific about which situations your solution should work for -- all valid HTML? – Tim Oct 11 '11 at 16:45

2 Answers2

3

As Donut says, you seriously shouldn't use regexes on HTML. However, since this is a pretty straightforward case I'll be an enabler. But seriously, if it gets one iota more complicated, switch to a DOM parser.

value="(.+?)"

I'm assuming you are using PHP, so to get the captured group out, do this:

preg_match('value="(.+?)"', $input, $groups);
echo "Value = " . $groups[1];

The ? makes it a lazy operator, so it grabs up to the first quotation mark. If there is the possibility of escaped quotation marks inside the quotation marks you need to add this:

value="(.+?[^\\])"
Chriszuma
  • 4,464
  • 22
  • 19
2

While it is generally not advisable to attempt to parse HTML with regular expressions, you could try this: value="([^"]*)".

Cajunluke
  • 3,103
  • 28
  • 28