Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Perl命名捕获组_Regex_Perl - Fatal编程技术网

Regex Perl命名捕获组

Regex Perl命名捕获组,regex,perl,Regex,Perl,我在正则表达式中创建了两个命名的捕获变量,第二个似乎不返回任何值,而第一个可以。我不知道为什么……这是密码 my $string = 'test [google] another test [windows]'; my $regex=qr/\w*\[{1}(?<firstBracket>\w+)\]{1}(?<secondBracket>\w*)/ip; $string=~ /$regex/; say $+{secondBracket}; my$string='t

我在正则表达式中创建了两个命名的捕获变量,第二个似乎不返回任何值,而第一个可以。我不知道为什么……这是密码

my $string = 'test [google] another test [windows]';

my $regex=qr/\w*\[{1}(?<firstBracket>\w+)\]{1}(?<secondBracket>\w*)/ip;

$string=~ /$regex/;

say $+{secondBracket};
my$string='test[google]另一个test[windows];
my$regex=qr/\w*\[{1}(?\w+\]{1}(?\w*)/ip;
$string=~/$regex/;
说$+{secondBracket};
我希望“第二个括号”会回来。 我可以做
$+{firstBracket}
,但第二个不行……有人能帮忙吗

谢谢。

你的意思可能是:

my $string = 'test [google] another test [windows]';

if( $string =~ /.*?\[(?<firstBracket>\w+)\].*?\[(?<secondBracket>\w+)\]/i ) {
        say $+{firstBracket};
        say $+{secondBracket};
}

my$re=qr/*?\[(?\w+)\].*?\[(?\w+)\]/i;
如果($string=~$re){
...
}
使用相同的输出…

您的意思可能是:

my $string = 'test [google] another test [windows]';

if( $string =~ /.*?\[(?<firstBracket>\w+)\].*?\[(?<secondBracket>\w+)\]/i ) {
        say $+{firstBracket};
        say $+{secondBracket};
}

my$re=qr/*?\[(?\w+)\].*?\[(?\w+)\]/i;
如果($string=~$re){
...
}

对于相同的输出…

模式中不描述空格
{1}
量词是无用的,请删除它们。不要在void上下文中使用
m/
。在使用任何捕获变量之前,请始终检查匹配是否成功。模式中不描述空格
{1}
量词是无用的,请删除它们。不要在void上下文中使用
m/
。在使用任何捕获变量之前,请始终检查匹配是否成功。谢谢…我还意识到,我没有包括第一个括号和第二个括号之间的空格!!愚蠢的谢谢你…我还意识到我没有包括第一个括号和第二个括号之间的空间!!愚蠢的
my $re = qr/.*?\[(?<firstBracket>\w+)\].*?\[(?<secondBracket>\w+)\]/i;
if( $string =~ $re ) {
    ...
}