Variable can be inserted in /patten/, similar to "$variable"
#!/usr/bin/perl
my $what="fred";
while(<>){
if(/^($what)/){
print "We saw $what in beginning of $_";
}
}
() can ctach the characters in it as varables, called $1, $2, $3 ....
The first () gets $1, second gets $2, .... caculate by the left (
$_="Hello there, neighbor";
if(/\s(\w+),/){
print "the word was $1\n"; # print "the word was there"
}
$_="Hello there, neighbor";
if(/(\S+) (\S+), (\S+)/){
print "words were $1 $2 $3\n";
}
The life sycle of a caught variable go until next successful match, which overide it.
If we only want () make group but not catch variable, we can use non-catch ().
$_="a saurus steak for dinner";
if(/(?:bronto)?saurus (steak|burger)/){
print "Fred wants a $1\n";
}
The ?: behind ( makes that () not catch variable.
We can name the caught variable instead of $1 $2 .... the numberic style.
my $names="Fred or Barney";
if($names=~/(?<name1>\w+) (?:and|or) (?<name2>\w+)/){
print "I saw $+{name1} and $+{name2}\n";
}
(?<LABLE>PATTEN) LABLE can be any name we like, all the names are stored in hash %+
Using the named variables we can insert more () not worrying the $number, \g{1} can also named as \g{lable}
my $names="Fred Flintstone and Wilma Flintstone";
if($names=~/(?<last_name>\w+) and \w+ \g{last_name}/){
print "I saw $+{last_name}\n"; # Flintstone
}
if("Hello there, neighbor"=~/\s(\w+),/){
print "That actually matched '$&'.\n";
}
if("Hello there, neighbor"=~/\s(\w+),/){
print "That was ($`) ($&) ($').\n";
}
$& is what /patten/ matches, $` is parts before $&, $' is parts after $&.
/a{m,n}/ matches 'a' appears m to n times
/a{m,}/ matches 'a' appears at least m times
/a{m}/ matches 'a' appears exactly m times
RE has its connect order like this:
Patten Examples
() (...) (?:...) (?<LABLE>...)
* + ? {} a* a+ a? a{m,n}
^ $ abc ^a a$
| a|b|c
element a [abc] \d \1
Exersices:
1. Write a program, make it match 'match' and print the parts before and after it.
#!/usr/bin/perl
while(<>){
if(/match/){
print "That was |$`<$&>$'|\n";
}
}
##########################
2. Write a program, make it match words(made of \w) end with 'a'.
#!/usr/bin/perl
while(<>){
chomp;
if(/\w+a\b/){
print "That was |$`<$&>$'|\n";
}
}
##########################
3. Modify last program, catch the words end with 'a' in $1. And print it like: $1 contains 'Wilma'.
#!/usr/bin/perl
while(<>){
chomp;
if(/(\w+a\b)/){
print "\$1 contains '$1'\n";
}
}
##########################
4. Modify last program, catch words into named variable. And print it like: 'word' contains 'Wilma'.
#!/usr/bin/perl
while(<>){
chomp;
$lable="word";
if(/(?<$lable>\w+a\b)/){
print "'$lable' contains '$+{$lable}'\n";
}
}
##########################
5. Modify last program, print the word ended with 'a' and the following 5 characters(if there are).
#!/usr/bin/perl
while(<>){
chomp;
if(/(?<word>\w+a\b)(?<more>.{0,5})/){
print "$+{word}|$+{more}\n";
}
}
##########################
6. Write a new program, print all lines ended with sapce character(s), print more character(s) to make it human readable.
#!/usr/bin/perl
while(<>){
chomp;
if(/\s+$/){
print "$_|\n";
}
}
##########################