开发者

Perl regular expression to match everything between double quotes?

The text is:

some text here x"/get/me/out/of/here.png"

How can I get everything between the double quotes?

This seems not to work:

if ($text =~ /".+?"/) {
 print"found: $1开发者_如何转开发\n";
}

Thanks.


You need to have a $1, so wrap parenthesis around.+?:

$text = 'foo "bar baz" mu';

if ($text =~ /"(.+?)"/) {
  print"found: $1\n";
}

prints:

bar baz

Or use $& to grab the entire match ("bar baz": including the double quotes):

if ($text =~ /".+?"/) {
  print"found: $&\n";
}


As always, any time you think that a Regex is the best solution (and occasionally it might be), a quick trip to CPAN may find you something better. In this case Text::Balanced. There is the extract_delimited or extract_quotelike function which will extract text between specified quotes.

Read the instructions carefully, as it has useful but perhaps unexpected behaviors. However it can be very useful, especially if you have to do repeated extractions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜