第八章: 以正则表达式进行匹配

#! /usr/bin/perl
use strict;
use warnings;

#my @strict_pragma = qw ( a b c);
#print $strict_pragma[0];

print "\n----------------------------------_delimiter_match_http://--------------------------\n";
$_ = "https://www.cnblogs.com/books2read/";
if(m%http[s]*://%){
    print "unpacking_@_ * 1\n";
}
print "\n----------------------------------_delimiter_match_http://--------------------------\n";

print "\n----------------------------------_optional_modifiers_----------------------------------------------------------------------------------------------\n";
print "unnecessary subscripting:";
chomp($_ = <STDIN>);
if(/yes/i){
    print "array-ref values.\n"
}
print "\n----------------------------------_/s--------------------------\n";
$_ = " naming conventions references to slice factoring\nslicing anonymous attribute demolition.";
if(/conventions.*demolition/s){
    print "string mentions demolition after conventions.\n";
}
print "\n----------------------------------_/s--------------------------\n";
print "\n----------------------------------_optional_modifiers_----------------------------------------------------------------------------------------------\n";

print "\n----------------------------------_/x--------------------------\n";
$_ = "demolition array-ref values.  2";
if(/
    -?  # one optional minus sign
    \d+ # one or more digits before the decimal point
    \.? # an optional decimal point
    \d* # some optional digits after the decimal point
/x){
    print "match *1.\n"
}
$_ = "3415";
if(/
    -?  # one optional minus sign
    \d+ # one or more digits before the decimal point
    \.? # an optional decimal point
    \d* # some optional digits after the decimal point
/x){
    print "match *2.\n"
}
print "\n----------------------------------_/x--------------------------\n";

print "\n----------------------------------_combining_opiton_modfiers--------------------------\n";
$_ = " naming conventions references to slice factoring\nslicing anonymous attribute Demolition.";
if(/conventions.*demolition/si){
    print "string mentions demolition after conventions. match*3\n";
}
print "\n----------------------------------_combining_opiton_modfiers--------------------------\n";

print "\n----------------------------------2222_combining_opiton_modfiers--------------------------\n";
$_ = " naming conventions references to slice factoring\nslicing anonymous attribute Demolition.";
if(/
    conventions
    .*
    demolition
    /six){
    print "string mentions demolition after conventions. match*4\n";
}
print "\n----------------------------------222_combining_opiton_modfiers--------------------------\n";

print "\n----------------------------------_anchors_--------------------------\n";
$_ = "ATTR marker,";
my $contextual;
if(/\s+(\w+),/){
    print "Caret anchor works.\n";
    $contextual = $1;
}
print "\n----------------------------------_anchors_--------------------------\n";

print "\n----------------------------------_persistence_of_memory--------------------------\n";
my $precedence = "!!!";
$precedence =~ /(\w+)/;
print "precedence's word was $1 ... or was it?\n";
# to prevent the matching variable override, you can store it in a defined variable
print $contextual;
print "\n----------------------------------_persistence_of_memory--------------------------\n";

print "\n----------------------------------_automatic_match_variable--------------------------\n";
$_ = "ATTR marker,";
if(/\s+(\w+),/){
    print "That actually matched '$&'.\n";
}
print "That was ($`)($&)($').\n";
print ($`);
print "\n";
print '$&'."\n";
print $'."\n";
print "\n";
print '$contextual';
print "\n";
print $contextual;
print "\n----------------------------------_automatic_match_variable--------------------------\n";

print "\n----------------------------------_general_quantifiers--------------------------\n";
$_ = ",,,,,,";
if(/,{5}/){
    print "match * 8\n";
}
print "\n----------------------------------_general_quantifiers--------------------------\n";

print "\n----------------------------------_pattern_test_program--------------------------\n";
$_ = "test, it's!";
if(/\w+,\s+\w+'/){
    print "matched: <$`><$&><$'>|\n";
}
print "\n----------------------------------_pattern_test_program--------------------------\n";

print "\n----------------------------------_ch8_Q1--------------------------\n";
$_ = "beforematchafter";
if(/match/){
    print "matched: <$`><$&><$'>|\n";
}
print "\n----------------------------------_ch8_Q1--------------------------\n";

print "\n----------------------------------_ch8_Q2--------------------------\n";
$_ = "beforematcha? fter";
if(/\b\w+a\b/){
    print "matched: <$`><$&><$'>|\n";
}
print "\n----------------------------------_ch8_Q2--------------------------\n";




















posted @ 2019-07-04 14:12  jimy1  阅读(309)  评论(0编辑  收藏  举报
Total Visits
AmazingCounters.com