📜  Perl正则表达式

📅  最后修改于: 2021-01-07 08:32:13             🧑  作者: Mango

Perl正则表达式

正则表达式是字符的字符串,其限定特定的图案。 perl正则表达式的语法与awk,grep和sed的语法非常相似。

perl中包含三个正则表达式运算符:

  • 匹配正则表达式运算符
  • 替代正则表达式运算符
  • 音译正则表达式运算符

Perl匹配运算符

Perl匹配运算符具有各种修饰符。它主要用于将字符串或语句与正则表达式匹配。

匹配运算符修饰符

Operators Description
cg Continue search even if the global match fails
g Search globally for all matches
i Search the match with case insensitivity
m If string has a new line character, the $ and ^ will match against a new line boundary instead of string boundary
o Allow expression evaluation only once
s Use . to match a new line character
x Use white space in the expression

Perl匹配运算符=〜

匹配运算符=〜用于匹配给定字符串中的单词。这是区分大小写的,这意味着如果字符串具有小写字母并且您正在搜索大写字母,则它将不匹配。

$line = "This is javaTpoint.";
if ($line =~ /java/){
   print "Matching\n";
}else{
   print "Not Matching\n";
}
if ($line =~ /Java/){
   print "Matching\n";
}else{
   print "Not Matching\n";
}

输出:

Matching
Not Matching

Perl匹配运算符!〜

它与较早版本(=〜)相反。如果字母匹配,则输出不匹配,反之亦然。

$ line = "This is javaTpoint.";
if ($line!~ /java/){
   print "Matching\n";
}else{
   print "Not Matching\n";
}
if ($line!~ /Java/){
   print "Matching\n";
}else{
   print "Not Matching\n";
}

输出:

Not Matching
Matching

$ _的Perl匹配运算符

您还可以将其与特殊的默认变量$ _匹配。

$_ = "This is javaTpoint.";
    if (/java/) {
        print "Matching\n";
    }
    else {
        print "Not Matching\n";
    }
if (/Java/) {
        print "Matching\n";
    }
    else {
        print "Not Matching\n";
    }

输出:

Matching
Not Matching

Perl匹配运算符与m

匹配运算符m也用于匹配给定字符串中的单词。

$ line = "This is javaTpoint.";
if ($line=~ m[java]){
   print "Matching\n";
}else{
   print "Not Matching\n";
}
if ($line=~ m{Java}){
   print "Matching\n";
}else{
   print "Not Matching\n";
}

输出:

Matching
Not Matching

Perl匹配运算符,含$ 1,$ 2 …

$ 1,$ 2将根据指定的括号print单词。

my $word = "CuNaHg";
    $word =~ /(((Cu)(Na))(Hg))/;
    print "1: $1 2: $2 3: $3 4: $4 5: $5 6: $6\n";

输出:

1: CuNaHg 2: CuNa 3: Cu 4: Na 5: Hg 6: 

Perl匹配运算符?

它从给定的字符串在括号内打印匹配的字符。

my $word = "CuNaHg";
    $word =~ /(?:(Cu)NaHg)/;
    print "$1\n"; # prints "Cu"
     $word =~ /(?:Cu(Na)Hg)/;
    print "$1\n"; # prints "Na"
     $word =~ /(?:CuNa(Hg))/;
    print "$1\n"; # prints "Hg?

输出:

Cu
Na
Hg 

Perl替代运算符

替换运算符只是匹配运算符的扩展。它允许替换与某些新文本匹配的文本。

其基本语法为:

s/oldPattern/newPattern /;

带s ///的Perl替代运算符

在这里,我们在第一部分中用s ///将液体替换为固体。

在第二部分中,以s /// g整体将“液体”替换为“固体”。

$line = "liquid will remain liquid until it is evaporated";
$line =~ s/liquid/solid/;
print "$line\n";
print"\n";
$line = "liquid will remain liquid until it is solidified";
$line =~ s/liquid/solid/g;
print "$line\n";

输出:

solid will remain liquid until it is evaporated
solid will remain solid until it is evaporated

Perl翻译运算符

翻译运算符与替换运算符符相似。但是转换不使用正则表达式来搜索替换值。

其基本语法为:

tr/oldLetter/newLetter /;

Perl翻译运算符替换一个字母

在这里,所有的“ l”字母将由翻译运算符替换为“ z”字母。

$line = "liquid will remain liquid until it is evaporated";
$line =~ tr/l/z/;
print "$line\n";

输出:

ziquid wizz remain ziquid untiz it is evaporated

Perl Translation Operator替换多个字母

在这里,翻译运算符会将所有的“ l”和“ i”字母替换为“ z”和“ x”字母。

$line = "liquid will remain liquid until it is evaporated";
$line =~ tr/li/zx/;
print "$line\n";

输出:

zxquxd wxzz remaxn zxquxd untxz xt xs evaporated