Reputation: 25538
I am trying to match the value of the following HTML snippet:
<input name="example" type="hidden" value="matchTextHere" />
with the following:
x = response.match(/<input name="example" type="hidden" value="^.+$" \/>/)[0]
why is this not working? it doesn't match 'matchTextHere'
edit:
when i use:
x = response.match(/<input name="example" type="hidden" value="(.+)" \/>/)[0]
it matches the whole html element, and not just the value 'matchTextHere'
Upvotes: 0
Views: 232
Reputation: 123831
you just need to change [0] to [1]
response='<input name="example" type="hidden" value="matchTextHere" />'
puts response.match(/<input name="example" type="hidden" value="(.*?)" \/>/)[1]
matchTextHere
Upvotes: 0
Reputation: 59451
^
matches start of a line and $
matches end of the line. Change ^.+$
to \w+
and it will work for values that doesn't contain any symbols. Make it a parenthetical group to capture the value - (\w+)
Update: to match anything between the quotes (assuming that there aren't any quotes in the value), use [^"]+
. If there are escaped quotes in the value, it is a different ballgame. .+
will work in this case, but it will be slower due to backtracking. .+
first matches upto the end of the string (because .
matches even a "
), then looks for a "
and fails. Then it comes back one position and looks for a "
and fails again - and so on until it finds the "
- if there was one more attribute after value
, then you will get matchTextHere" nextAttr="something
as the match.
x = response.match(/<input name="example" type="hidden" value="([^"]+)" \/>/)[1]
That being said, the regex will fail if there is an extra space between any of the attribute values. Parsing html with regex is not a good idea - and if you must use regex, you can allow extra spaces using \s+
/<input\s+name="example"\s+type="hidden"\s+value="([^"]+)"\s*\/>/
Upvotes: 3
Reputation: 46965
You don't need the ^ and $:
x = response.match(/<input name="example" type="hidden" value=".+" \/>/)[0]
Upvotes: 0
Reputation: 75704
Because you have a start-of-line token (^
) and an end-of-line token ($
) in your regular expression. I think you meant to capture the value, this might solve your problem: value="(.+?)"
.
Beware, though, that processing html with regular expressions is not a good idea, it can even drive you crazy. Better use an html parser instead.
Upvotes: 0