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

本文深入探讨Perl正则表达式的使用技巧,包括不同修饰符的作用、锚点的应用、匹配变量的利用等,通过实例演示如何高效处理字符串,是Perl程序员不可或缺的实战指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#! /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";




















转载于:https://www.cnblogs.com/books2read/p/11132118.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值