perl - 使用regex

postfix in perl

//i : case insensitive

#!/usr/bin/perl
use warnings;
$_ = 'This is a wilma line';
if (/WiLMa/i) {
	print "match\n";
}

Matches.
if (/WiLMa/) {, it doesn't match.

//m : multiline match

#!/usr/bin/perl
use warnings;
$_ = 'This is a wilma line
barney is on another line
but this ends in fred
and a final dino line';
if (/fred$/m) {
	print "match\n";
}

This matches.
if (/fred$/) { (without m), it doesn't match.
if (/fred\z/m) {, it doesn't match.
if (/fred\z/) {, it doesn't match.

The function of $ without m is identical to \z, the ending anchor.

Similar the caret symbol ^, which is defined in regex.

//x : adding white space

#!/usr/bin/perl
use warnings;
$_ = 'This is a wilma line';
if (/wilma \s line/x) {
	print "match\n";
}

Matches.
if (/wilma \s line/) {, it doesn't match.

Anchors

\A

\A anchor matches the absolute beginning of a string.

\z

\z anchor matches the end of a string without a new line.

while (<STDIN>) {
print if /\.png\Z/;
}

\Z

\Z anchor matches the end of a string with an optinal new line.

while (<STDIN>) {
chomp;
print "$_\n" if /\.png\z/;
}

This matches *.png from interactive command prompt,
while it dodoesn't matches if there is no chomp

\b

The word-boundary anchor \b matches at either end of a word.

#!/usr/bin/perl
use warnings;
$_ = 'neofred';
if (/fred\b/) {
	print "match\n";
}

It matches,
but with, /\bfred\b/, it doesn't match.

\w

\w anchor matches ordinary letters, digits, and underscores.
It identical to [0-9a-zA-Z_]

\d

\d anchor matches digital numbers of 0-9.

Perl match variables

(?:)

Non-capturing parentheses, just grouping.

()

Capturing parentheses.

(?<name>)

Naming capture.

To assginment, use $+{name}.

Automatic match section

$&

Entire matched section.

$ (Invalid in CNBLOGS)

Before mathced.

$'

After matched.

posted @ 2023-05-24 09:51  devindd  阅读(13)  评论(0编辑  收藏  举报