Regex 如何在正则表达式中引用匹配的部分

Regex 如何在正则表达式中引用匹配的部分,regex,perl,Regex,Perl,我使用下面的代码来搜索一个子字符串,并在其前后使用几个字符将其打印出来。不知何故,Perl对我使用$1表示不满,并抱怨 在串联(.)或字符串中使用未初始化值$1 我不明白为什么…你能吗 use List::Util qw[min max]; my $word = "test"; my $lines = "this is just a test to find something out"; my $context = 3; while ($lines =~ m/\b$word\b/g ) { #

我使用下面的代码来搜索一个子字符串,并在其前后使用几个字符将其打印出来。不知何故,Perl对我使用
$1
表示不满,并抱怨

在串联(.)或字符串中使用未初始化值
$1

我不明白为什么…你能吗

use List::Util qw[min max];
my $word = "test";
my $lines = "this is just a test to find something out";
my $context = 3;
while ($lines =~ m/\b$word\b/g ) { # as long as pattern is found...
    print "$word\ ";
    print "$1";
    print substr ($lines, max(pos($lines)-length($1)-$context, 0), length($1)+$context); # check: am I possibly violating any boundaries here
}

您必须使用括号将
$word
捕获到正则表达式组
$1

while ($lines =~ m/\b($word)\b/g)

您必须使用括号将
$word
捕获到正则表达式组
$1

while ($lines =~ m/\b($word)\b/g)

当您使用
$1
时,您要求代码使用从正则表达式中捕获的第一个组,因为您的正则表达式没有任何组,所以该变量将不存在

您可以使用
$&
引用整个匹配,也可以将捕获组添加到正则表达式中并继续使用
$1

i、 e.任何一方:

use List::Util qw[min max];
my $word = "test";
my $lines = "this is just a test to find something out";
my $context = 3;
while ($lines =~ m/\b$word\b/g ) { # as long as pattern is found...
    print "$word\ ";
    print "$&";
    print substr ($lines, max(pos($lines)-length($&)-$context, 0), length($&)+$context); # check: am I possibly violating any boundaries here
}


注意:在这里使用
(\b$word\b)
(\b$word)\b
\b($word\b)
\b($word)\b
并不重要,因为
\b
是长度为0的“字符串”。

当使用
$1
时,您要求代码使用从regex捕获的第一个组,因为您的regex没有任何组,所以该变量将不存在

您可以使用
$&
引用整个匹配,也可以将捕获组添加到正则表达式中并继续使用
$1

i、 e.任何一方:

use List::Util qw[min max];
my $word = "test";
my $lines = "this is just a test to find something out";
my $context = 3;
while ($lines =~ m/\b$word\b/g ) { # as long as pattern is found...
    print "$word\ ";
    print "$&";
    print substr ($lines, max(pos($lines)-length($&)-$context, 0), length($&)+$context); # check: am I possibly violating any boundaries here
}


注意:在这里使用
(\b$word\b)
(\b$word)\b
\b($word\b)
\b($word)\b
并不重要,因为
\b
是一个长度为0的“字符串”。

当您想在regex中寻址匹配的部分时,请将其放在括号中。然后,您就可以通过
$1
变量(对于第一对括号)、
$2
(对于第二对括号)等来处理此数学部分。

当您要在regex中处理匹配的部分时,请将其放在括号中。然后,您可以通过
$1
变量(对于第一对括号)、
$2
(对于第二对括号)等来处理此数学部分。

$1
$2
等保存捕获组找到的字符串。执行匹配时,所有这些变量都设置为未定义。问题中的代码没有任何捕获组,因此,
$1
从未给定值,它是未定义的

use strict;
use warnings;

sub show
{
    printf "\$1: %s\n", (defined $1 ? $1 : "-undef-");
    printf "\$2: %s\n", (defined $2 ? $2 : "-undef-");
    printf "\$3: %s\n", (defined $3 ? $3 : "-undef-");
    print "\n";
}

my $text = "abcdefghij";
show();

$text =~ m/ab(cd)ef(gh)ij/;  # First match
show();

$text =~ m/ab(cd)efghij/;  # Second match
show();

$text =~ m/abcdefghij/;  # Third match
show();
运行下面的代码可以显示效果。最初未定义
$1
$2
$3
。第一个匹配设置
$1
$2
,但不设置
$3
。第二个匹配项仅设置
$1
,而不是将
$2
清除为未定义。第三场比赛没有捕获组,三个组都没有定义

use strict;
use warnings;

sub show
{
    printf "\$1: %s\n", (defined $1 ? $1 : "-undef-");
    printf "\$2: %s\n", (defined $2 ? $2 : "-undef-");
    printf "\$3: %s\n", (defined $3 ? $3 : "-undef-");
    print "\n";
}

my $text = "abcdefghij";
show();

$text =~ m/ab(cd)ef(gh)ij/;  # First match
show();

$text =~ m/ab(cd)efghij/;  # Second match
show();

$text =~ m/abcdefghij/;  # Third match
show();

$1
$2
等保存捕获组找到的字符串。执行匹配时,所有这些变量都设置为未定义。问题中的代码没有任何捕获组,因此,
$1
从未给定值,它是未定义的

use strict;
use warnings;

sub show
{
    printf "\$1: %s\n", (defined $1 ? $1 : "-undef-");
    printf "\$2: %s\n", (defined $2 ? $2 : "-undef-");
    printf "\$3: %s\n", (defined $3 ? $3 : "-undef-");
    print "\n";
}

my $text = "abcdefghij";
show();

$text =~ m/ab(cd)ef(gh)ij/;  # First match
show();

$text =~ m/ab(cd)efghij/;  # Second match
show();

$text =~ m/abcdefghij/;  # Third match
show();
运行下面的代码可以显示效果。最初未定义
$1
$2
$3
。第一个匹配设置
$1
$2
,但不设置
$3
。第二个匹配项仅设置
$1
,而不是将
$2
清除为未定义。第三场比赛没有捕获组,三个组都没有定义

use strict;
use warnings;

sub show
{
    printf "\$1: %s\n", (defined $1 ? $1 : "-undef-");
    printf "\$2: %s\n", (defined $2 ? $2 : "-undef-");
    printf "\$3: %s\n", (defined $3 ? $3 : "-undef-");
    print "\n";
}

my $text = "abcdefghij";
show();

$text =~ m/ab(cd)ef(gh)ij/;  # First match
show();

$text =~ m/ab(cd)efghij/;  # Second match
show();

$text =~ m/abcdefghij/;  # Third match
show();

$1
将没有价值,除非您实际捕获了某些内容

可以将边界收集方法调整为使用“向前看”和“向后看”

use strict;
use warnings;

my $lines = "this is just a test to find something out";
my $word = "test";
my $extra = 10;

while ($lines =~ m/(?:(?<=(.{$extra}))|(.{0,$extra}))\b(\Q$word\E)\b(?=(.{0,$extra}))/gs ) {
    my $pre = $1 // $2;
    my $word = $3;
    my $post = $4;
    print "'...$pre<$word>$post...'\n";
}
使用严格;
使用警告;
my$lines=“这只是一个发现问题的测试”;
我的$word=“test”;
我的$extra=10;

而($lines=~m/(?:(?
<1
将没有价值,除非您实际捕获了一些东西

可以将边界收集方法调整为使用“向前看”和“向后看”

use strict;
use warnings;

my $lines = "this is just a test to find something out";
my $word = "test";
my $extra = 10;

while ($lines =~ m/(?:(?<=(.{$extra}))|(.{0,$extra}))\b(\Q$word\E)\b(?=(.{0,$extra}))/gs ) {
    my $pre = $1 // $2;
    my $word = $3;
    my $post = $4;
    print "'...$pre<$word>$post...'\n";
}
使用严格;
使用警告;
my$lines=“这只是一个发现问题的测试”;
我的$word=“test”;
我的$extra=10;

虽然($lines=~m/(?:)行得通,你能解释一下为什么需要这样做吗?@user1769925 check group capturing=>这是基本的正则表达式功能。谢谢!行得通,你能解释一下为什么需要这样做吗?@user1769925 check group capturing=>这是基本的正则表达式功能。