Perl 正则表达式匹配双引号之间的所有内容?
文本是:
some text here x"/get/me/out/of/here.png"
如何获取双引号之间的所有内容?
这似乎不起作用:
if ($text =~ /".+?"/) {
print"found: $1\n";
}
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要有一个
$1
,因此请用括号括住.+?
:prints:
或使用
$&
来获取整个匹配项 (< code>“bar baz”:包括双引号):You need to have a
$1
, so wrap parenthesis around.+?
:prints:
Or use
$&
to grab the entire match ("bar baz"
: including the double quotes):与往常一样,每当您认为正则表达式是最佳解决方案(有时可能是)时,快速访问 CPAN 可能会为您找到更好的解决方案。在本例中
Text::Balanced
。有extract_delimited
或extract_quotelike
函数可以提取指定引号之间的文本。仔细阅读说明,因为它有有用但可能意想不到的行为。然而,它可能非常有用,特别是当您必须重复提取时。
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 theextract_delimited
orextract_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.