Wednesday, October 26, 2011

Perl - Pattern Matching [ regular expression ]

Metacharacter: Actually, they are very useful and have special meanings within the patterns.

/ | () [] {} ^ $ * +? .

\: The backslash and a wildcard that has the 'power' to transform into something special character literal

e.g. \ b \ D \ t \ 3

Pattern

#!/usr/bin/perl

use strict;
use warnings;

my $text = "Actually, they are very useful and have special meanings within the patterns.\n";
print $text if ($text =~ / special | WhichEitherStandard /);

$text = "thethe men the Women the children\n";
print $text if ($text =~ /(the){2}/);


$text = "theee men the Women the children\n";
print $text if ($text =~ /the{3}/);



$text = "the men the Women the children\n";
print $text if ($text =~ /^the/);


$text = "the men the Women the children ...the\n";
print $text if ($text =~ /the$/);


$text = "Perl -> windows \n";
print $text ;
$text =~ s/windows/linux/;
print $text ;


$text = "Perl -> linux \n";
$text =~ tr/linux/l***x/;
print $text ;

$text = "encrypted key\n";
print $text ;
$text =~ tr/a-zA-Z/n-za-mN-ZA-M/;
print $text ;


result 


$perl  command.pl 
Actually, they are very useful and have special meanings within the patterns.
thethe men the Women the children
theee men the Women the children
the men the Women the children
the men the Women the children ...the
Perl -> windows 
Perl -> linux 
Perl -> l***x 
encrypted key
rapelcgrq xrl

Quantifiers :   Quantifiers only make sense that an atom attached to.


*  +  ?  *?  {3}  {1,2}

0 comentários:

Post a Comment