perl - Regex to capture substring between quotes -


i have following piece of code works: (a standard text string)

($txid) = $content =~ m/ start (.*) stop /; print $txid; 

and following piece of code doesn't: (a string quotation marks)

($txid) = $content =~ m/<input name="transactionid" value="(.*)" type="hidden">/; print $txid; 

i'm guessing problem being caused quotation marks. know how can solve this?

by default, * quantifier "greedy", means (.*) match possibly can. restrict such matches little possible, i.e. until next double-quote occurs in string, add ? it, thus:

($txid) = $content =~ m/<input name="transactionid" value="(.*?)" type="hidden">/; 

assuming value of value attribute contains no escaped double quotes, produce result you're after.

(of course, in cases there are escaped double quotes, there no regex reliably extract string of interest , nothing else. why people tell not use regexes sole tool parsing balanced text. on other hand, given simplicity of purpose here, you'll away -- anything, , mean anything, requires html or xml handling that's more complex this, use xml parser.)


Comments