Perl语言入门:第九章 使用正则表达式处理文本 示例程序和代码
#! /usr/bin/perl
use strict;
use warnings;
print "\n----------------------------------_substitutions_with_s///--------------------------\n";
$_ = "He's out bowling with Barney tonight.";
s/Barney/Fred/;
print $_."\n";
s/filehandles/vertical_alignment/;
print $_."\n";
s/with (\w+)/against $1/;
print $_."\n";
print "\n----------------------------------_substitutions_with_s///--------------------------\n";
print "\n----------------------------------_/g--------------------------\n";
$_ = " xxx";
s/^\s+//;
print "<$_>\n";
$_ = "xxx ";
s/\s+$//;
print "<$_>\n";
$_ = " xxx ";
s/^\s+|\s+$//g; # alternatives zeyipipei
print "<$_>\n";
print "\n----------------------------------_/g--------------------------\n";
print "\n----------------------------------_case_shifting--------------------------\n";
$_ = "I saw Cumulative with metHods.";
s/(cumulative|methods)/\U($1)/gi;
print "<$_>\n";
s/(cumulative|methods)/\L($1)/gi;
print "<$_>\n";
$_ = "I saw Cumulative with metHods.";
s/(\w+) with (\w+)/\U$2 with $1/;
print "<$_>\n";
s/(\w+) WITH (\w+)/\L$2 with $1/;
print "L:<$_>\n";
s/(\w+) with (\w+)/\u$2 with $1/g;
print "39: <$_>\n";
s/(cumulative|methods)/\u\L$1/g;
print "41: <$_>\n";
my $automating_sort = "factoring_out";
print "Hi \L\u$automating_sort\E, how are you?\n";
print "\n----------------------------------_case_shifting--------------------------\n";
print "\n----------------------------------_split_operator--------------------------\n";
my @globbing = split /:/, "homonyms:parentheses:reversing_lists";
print @globbing."\n";
my @variable_width_data = split /\s+/, "Mmap module preserving between sorts SHA values.";
print @variable_width_data;
print "\n";
$_ = "xxl Mmap module preserving between sorts SHA values.";
my @heredocs = split;
print @heredocs;
print "\n";
print "\n----------------------------------_split_operator--------------------------\n";
print "\n----------------------------------_join--------------------------\n";
my $caught_method = "-";
my $carp_module = join $caught_method, @globbing;
print $carp_module."\n";
my $charnames_module = join "chroot", "chunking";
print $charnames_module."\n";
print "\n----------------------------------_join--------------------------\n";
print "\n----------------------------------_m//_in_list_context_--------------------------\n";
$_ = "class_hierarchies, demolition, autoload";
my($automating, $blessing, $objects) = /(\S+), (\S+), (\S+)/;
print $automating, $blessing, $objects;
print "\n";
my $text = "Fred dropped a 5 ton granite block on Mr. Slate";
my @words = ($text =~ /\w+/ig);
my @words___ = ($text =~ /[a-z]/ig);
print @words;
print "\n";
print @words___;
print "\n";
print "\n----------------------------------_m//_in_list_context_--------------------------\n";
print "\n----------------------------------_nongreedy_quantifiers--------------------------\n";
$_ = "I thought you said Fred and <BOLD>Velma</BOLD>, not <BOLD>Willa</BOLD>";
s#<BOLD>(.*?)</BOLD>#$1#g;
print $_."\n";
$_ = "I'm much better\nthan Barney is\nat bowling, \nWilma.\n";
print "Found 'Wilma' at start of line\n" if /^Wilma/im;
print "\n----------------------------------_nongreedy_quantifiers--------------------------\n";
=head1
print "\n----------------------------------_matching_multiple-line_text--------------------------\n";
open FILE, "< file_4_ex_ch7.txt"
or die "cannot open file: $!";
my $lines = join '', <FILE>;
print $lines."\n";
$lines =~ s/^/file_4_ex_ch7.txt: /gm;
print $lines;
print "\n----------------------------------_matching_multiple-line_text--------------------------\n";
print "\n----------------------------------_updating_many_files--------------------------\n";
chomp(my $date = `date`);
$^I = ".bak";
while(<>){
s/Author:.*/Author: Wei Yan/;
s/^Phone.*\n//s;
s/Date.*/Date: $date/;
print;
}
print "\n----------------------------------_updating_many_files--------------------------\n";
=cut
print "\n----------------------------------_?=--------------------------\n";
my $string="I love chocolate ice.";
$string =~ s/chocolate(?= ice)/vanilla/;
print "$string\n";
print "\n----------------------------------_?=--------------------------\n";
print "\n----------------------------------_test1_triple_match--------------------------\n";
my $what = "string_evaluations";
$_ = "string_evaluationsstring_evaluationsstring_evaluations";
$what = "utilities|Mmap";
$_ = "utilitiesMmaputilitiesMmap";
if(/($what){4}/){
print "$1 matched.\n";
}
print "\n----------------------------------_test1_triple_match--------------------------\n";
print "\n----------------------------------_--------------------------\n";
Perl语言入门:第九章 使用正则表达式处理文本 示例程序和代码的更多相关文章
- C语言入门---第九章 C语言指针
没学指针就是没学C语言! 指针是C语言的精华,也是C语言的难点. 所谓指针,也就是内存的地址,所谓指针变量,也就是保存了内存地址的变量.不过人们往往不会区分两者的概念,而是混淆在一起使用. ===== ...
- Perl语言入门(中文版)(第6版) 东南大学出版社
第一章简介 问题与答案 这本书适合你吗? 为何有这么多的脚注? 关于习题和解答? 习题前标的数字是什么意思? 如果我是Perl讲师? “Perl”这个词表示什么意思? Larry为什么要创造Perl? ...
- Perl 语言入门6-9
---- 第6章 哈希----------- 简介 键值对.键和值都是任意标量,但键总是会被转换成字符串. 键唯一,值可重复. 应用场景:一组数据对应到另一组数据时. 如找出重复/唯一/交叉引用/查表 ...
- Perl语言入门: 斜线不是元字符,所以在不作为分隔符时不需要加上反斜线。
Perl语言入门: 斜线不是元字符,所以在不作为分隔符时不需要加上反斜线.
- Cesium入门2 - Cesium环境搭建及第一个示例程序
Cesium入门2 - Cesium环境搭建及第一个示例程序 Cesium中文网:http://cesiumcn.org/ | 国内快速访问:http://cesium.coinidea.com/ 验 ...
- Perl语言入门14-17
---------第十四章 字符串与排序------------------- index查找子字符串 my $stuff = "howdy world!"; my $where ...
- Perl语言入门
Perl 是 Practical Extraction and Report Language 的缩写,可翻译为 "实用报表提取语言". Perl语法基础: (1)Perl程序由声 ...
- 深入理解Magento-第九章-修改、扩展、重写Magento代码
(博主提示:本章应该不是原作者的第九章,仅作补充和参考) 作为一个开发者的你,肯定要修改Magento代码去适应你的业务需求,但是在很多时候我们不希望修改Magento的核心代码,这里有很多原因,例如 ...
- Perl语言入门:第六章习题:处理用户所指定的名字并汇报相应的姓。
37 print "\n----------------------------------_exercise_6_1--------------------------\n"; ...
随机推荐
- Python风格规范分享
今天给大家分享Python 风格规范,以下代码中 Yes 表示推荐,No 表示不推荐. 分号 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 每行不超过80个字符 以下情况除外: 长的 ...
- odoo 日志文件太大处理,logfile自动轮替
可以在conf 文件中设置配置信息: logfile = /your/logfile/path/ logrotate = True ; 设置为True 即可自动更换旧的日志文件, 参考: https: ...
- POJ 3050:Hopscotch
Hopscotch Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2506 Accepted: 1784 Descrip ...
- Linux下mysql5.7安装
当前最新版本为5.7,此次将分别采用yum安装和tar包编译安装的方式分别说明. 一.Yum安装 A:获取repo源 [root@localhost ~]# wget http://dev.mysql ...
- Mac Outlook 2016 无法打开会议室日历
问题:Mac Outlook 2016 无法打开会议室日历信息,报错截图如下: 解决方案: Set-MailboxFolderPermission -Identity XXX@xxx.com:\日历 ...
- Restful设计相关
1.get,从服务器获取资源. 2.post 新增资源. 3.put 更新完整资源. 4.patch 更新部分资源. 5. delete 删除资源.
- 寒假day14
今天去医院看脸了,回来继续写论文.
- 2019牛客网暑假多校训练第四场 K —number
链接:https://ac.nowcoder.com/acm/contest/884/K来源:牛客网 题目描述 300iq loves numbers who are multiple of 300. ...
- Julia1.x安装
删除julia-1.0 $ jupyter kernelspec list Available kernels: julia-1.0 /Users/keke.zhaokk/Library/Jupyte ...
- UML-领域模型-添加关联和属性
1.何谓关联? 关联(association):一个类的全局变量引用了另一个类,就表示关联了这个类 2.何时使用关联? 长时间(需要记住)留存的需要关联:短时间的不需要.比如: 需要关联:老师教那些课 ...