Regex 将Perl中的字符串与空格匹配或不与空格匹配
我需要打印teststring和teststring,你能帮我吗?谢谢。这将适用于您的特定示例Regex 将Perl中的字符串与空格匹配或不与空格匹配,regex,perl,Regex,Perl,我需要打印teststring和teststring,你能帮我吗?谢谢。这将适用于您的特定示例 $search_buffer="this text has teststring in it, it has a Test String too"; @waitfor=('Test string','some other string'); foreach my $test (@waitfor) { eval ('if (lc $search_buffer =
$search_buffer="this text has teststring in it, it has a Test String too";
@waitfor=('Test string','some other string');
foreach my $test (@waitfor)
{
eval ('if (lc $search_buffer =~ lc ' . $test . ') ' .
'{' .
' $prematch = $`;' .
' $match = $&; ' .
' $postmatch = ' . "\$';" .
'}');
print "prematch=$prematch\n";
print "match=$match\n"; #I want to match both "teststring" and "Test String"
print "postmatch=$postmatch\n";
}
my $search_buffer="this text has teststring in it, it has a Test String too";
my $pattern = qr/test ?string/i;
say "Match found: $1" while $search_buffer =~ /($pattern)/g;
基本上,它将空格标记为可选的[\s]?
。
我所看到的问题是,它要求您知道您正在搜索的字符串中可能存在空格的确切位置
注意:您可能还必须使用不区分大小写的标志,即
/Test[\s]?String/i
这适用于您的特定示例
my $search_buffer="this text has teststring in it, it has a Test String too";
my $pattern = qr/test ?string/i;
say "Match found: $1" while $search_buffer =~ /($pattern)/g;
基本上,它将空格标记为可选的[\s]?
。
我所看到的问题是,它要求您知道您正在搜索的字符串中可能存在空格的确切位置
注意:您可能还必须使用不区分大小写的标志,即
/Test[\s]?String/i
这是一段可怕的代码。为什么要使用eval
并尝试将字符串连接到代码中,记住插入一些变量而忘记了一些变量?在这种情况下,根本没有理由使用eval
我假设您使用lc
试图使匹配不区分大小写。最好在正则表达式上使用/i
修饰符:
test\s?string
在您的例子中,您正在尝试将一些字符串与另一个字符串进行匹配,并且您希望补偿大小写和内部可能的空白。我假设字符串是以某种方式生成的,而不是硬编码的
您可以做的只是使用/x
修饰符,这将使正则表达式中的文本空白被忽略
您应该考虑的是字符串中的元字符。例如,如果您有一个字符串,如foo?
,则元字符?
将改变正则表达式的含义。您可以使用\Q\E
转义序列
所以解决办法是
$search_buffer =~ /$test/i; # case insensitive match
输出:
use strict;
use warnings;
use feature 'say';
my $s = "this text has teststring in it, it has a Test String too";
my @waitfor= ('Test string','some other string', '#test string');
for my $str (@waitfor) {
if ($s =~ /\Q$str/xi) {
say "prematch = $`";
say "match = $&";
say "postmatch = $'";
}
}
注意,我使用
prematch = this text has teststring in it, it has a
match = Test String
postmatch = too
这两个pragma对于学习如何编写好的Perl代码至关重要,没有(有效)理由在没有它们的情况下编写代码。这是一段可怕的代码。为什么要使用
eval
并尝试将字符串连接到代码中,记住插入一些变量而忘记了一些变量?在这种情况下,根本没有理由使用eval
我假设您使用lc
试图使匹配不区分大小写。最好在正则表达式上使用/i
修饰符:
test\s?string
在您的例子中,您正在尝试将一些字符串与另一个字符串进行匹配,并且您希望补偿大小写和内部可能的空白。我假设字符串是以某种方式生成的,而不是硬编码的
您可以做的只是使用/x
修饰符,这将使正则表达式中的文本空白被忽略
您应该考虑的是字符串中的元字符。例如,如果您有一个字符串,如foo?
,则元字符?
将改变正则表达式的含义。您可以使用\Q\E
转义序列
所以解决办法是
$search_buffer =~ /$test/i; # case insensitive match
输出:
use strict;
use warnings;
use feature 'say';
my $s = "this text has teststring in it, it has a Test String too";
my @waitfor= ('Test string','some other string', '#test string');
for my $str (@waitfor) {
if ($s =~ /\Q$str/xi) {
say "prematch = $`";
say "match = $&";
say "postmatch = $'";
}
}
注意,我使用
prematch = this text has teststring in it, it has a
match = Test String
postmatch = too
这两个pragma对于学习如何编写好的Perl代码至关重要,没有(有效)理由在没有它们的情况下编写代码。为什么要使用它们?此代码在
eval
之外的工作方式几乎相同。主要区别在于eval
版本速度较慢,并且存在潜在的安全漏洞。为什么要使用?此代码在eval
之外的工作方式几乎相同。主要区别在于eval
版本较慢,并且可能存在安全漏洞。sequence\s不是空格字符,而是采用单个单元格的不可见字符的字符类。它被称为白色字符。例如,它将空格和制表符以及其他一些字符匹配在一起。sequence\s不是空格字符,而是采用单个单元格的不可见字符的字符类。它被称为白色字符。例如,它将空格和制表符以及其他一些字符匹配在一起。