正则表达式捕获“...”的内容

时间:2014-10-23 19:19:54

标签: regex perl

我想在我的字符串中前两次出现"之间提取字符。

示例:

my $string = 's.w;;wq[]][.zaw"this is what I need"as.w;;wq[]][.zaw"this I do not need".w;;wq[]][.za';

提前感谢您解决我的问题:)

2 个答案:

答案 0 :(得分:0)

看看perlretut - Using Character Classes

正如所示:

use strict;
use warnings;

my $string = q{s.w;;wq[]][.zaw"this is what I need"as.w;;wq[]][.zaw"this I do not need".w;;wq[]][.za};

if ( $string =~ /"([^"]*)/ ) {
    print "$1\n";
}

输出:

this is what I need

或者只使用split

my $desire = ( split /"/, $string )[1];

答案 1 :(得分:0)

正则表达式是:/"([^"]*)"/

my ($wanted_string) = $string =~ /"([^"]*)"/;