珀尔 |正则表达式中的分组和交替
正则表达式或正则表达式是Perl编程的重要组成部分。它用于搜索指定的文本模式。在这种情况下,一组字符一起形成了搜索模式。它也被称为正则表达式。随着功能的增加,使用正则表达式可能会变得复杂。为了降低复杂度,Perl 为我们提供了更多的操作,例如交替和分组。
交替
这个名字本身就暗示了它所做的机制,首先它就像一个出现在“C”编程语言中的 (&&)运算符。 C 中的 '&&'运算符一直工作,直到它发现所有条件语句都为真,它不会返回 1,并且一旦找到假语句就返回 0。 Perl 中的交替同时以两种方式起作用。可以使用元字符“|”来完成交替或者 '[]'。以下示例将使事情变得清晰:
示例 1:
perl
#!/usr/bin/perl
# Initializing the string and checking it
# against few search patterns
my $str = "geeksforgeeks rocks...";
# prints 1 due to no match (=0)
print "1\n" if (($str =~ /gek|for/) == 0);
# for word search
if($str =~ /geek|for/)
{
print "I found $&.\n";
}
else
{
print "no match"
}
perl
#!/usr/bin/perl
# Initializing the string and checking it
# against few search patterns
my $str = "geeksforgeeks rocks...";
print "1\n" if ($str =~ /for|eeks|rock|ge/);
# | for word search
if($str =~ /for|eeks|rock|ge/)
{
print "I found $&.\n";
}
# for single character search
if ($str =~ /[gfrkc]/)
{
print "$&"
}
perl
#!/usr/bin/perl
# Initializing the string and checking it
# against few search patterns
my $str = "Blackbackground Brownbackground";
print "1\n" if ($str =~ /(Bron|Back)[a-z]+/);
if ($str =~ /(Brown|Black)[a-z]+/)
{
print "I found $&.\n";
}
else
{
print "no match"
}
perl
#!/usr/bin/perl
use warnings;
use strict;
# Initializing the string and checking it
# against few searching patterns
# array of strings
my @mail = ('Ab-@gmail.com',
'd.f@yahoo.com',
'0b_f@bing.com',
'jet@hotmail.con',
's/s@otmail.com');
for(@mail)
{
# search pattern to check valid mail
# service providers followed with '.com'
print "Valid : $_\n"
if($_ =~ /[a-zA-Z0-9-._]+@(gmail|yahoo|bing|hotmail)(.com)$/)
}
输出:
1
I found geek.
因此,在上面的示例中,它首先检查“geek”并在第一个替代模式中找到匹配项,一旦找到匹配项就返回 1。但它也会检查“for”,如果它不存在于字符串中,它不会被打扰,因为它已经返回了匹配状态。如果'for'匹配,它检查'for'是否出现在字符串的最小位置,因为'geeks'中的'g'出现在位置0,'for'中的'f'出现在位置5,'geek ' 存储在最后一个匹配模式中。查看以下示例:
$& - Contains the string matched by the last pattern match.
示例 2:
perl
#!/usr/bin/perl
# Initializing the string and checking it
# against few search patterns
my $str = "geeksforgeeks rocks...";
print "1\n" if ($str =~ /for|eeks|rock|ge/);
# | for word search
if($str =~ /for|eeks|rock|ge/)
{
print "I found $&.\n";
}
# for single character search
if ($str =~ /[gfrkc]/)
{
print "$&"
}
输出:
1
I found ge.
g
分组
分组用于在开头、结尾或中间搜索以相似单词或模式为界的模式,它还返回定位最小的模式。分组是使用元字符“()”完成的。以下是一个让事情变得清晰的例子:
示例 1:
perl
#!/usr/bin/perl
# Initializing the string and checking it
# against few search patterns
my $str = "Blackbackground Brownbackground";
print "1\n" if ($str =~ /(Bron|Back)[a-z]+/);
if ($str =~ /(Brown|Black)[a-z]+/)
{
print "I found $&.\n";
}
else
{
print "no match"
}
输出:
I found Blackbackground.
为了更好地理解,请查看以下示例:
perl
#!/usr/bin/perl
use warnings;
use strict;
# Initializing the string and checking it
# against few searching patterns
# array of strings
my @mail = ('Ab-@gmail.com',
'd.f@yahoo.com',
'0b_f@bing.com',
'jet@hotmail.con',
's/s@otmail.com');
for(@mail)
{
# search pattern to check valid mail
# service providers followed with '.com'
print "Valid : $_\n"
if($_ =~ /[a-zA-Z0-9-._]+@(gmail|yahoo|bing|hotmail)(.com)$/)
}
输出:
Valid : Ab-@gmail.com
Valid : d.f@yahoo.com
Valid : 0b_f@bing.com