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"; ...
随机推荐
- 吴裕雄--天生自然Django框架开发笔记:Django 模板
使用 django.http.HttpResponse() 来输出 "Hello World!".该方式将数据与视图混合在一起,不符合 Django 的 MVC 思想. Djang ...
- 201771010123汪慧和《面向对象程序设计Java》第十七周实验总结
一.理论部分 1.多线程并发执行中的问题 ◆多个线程相对执行的顺序是不确定的. ◆线程执行顺序的不确定性会产生执行结果的不确定性. ◆在多线程对共享数据操作时常常会产生这种不确定性. 2.线程的同步 ...
- Python爬虫之解析网页
常用的类库为lxml, BeautifulSoup, re(正则) 以获取豆瓣电影正在热映的电影名为例,url='https://movie.douban.com/cinema/nowplaying/ ...
- 学习Github必须要会的知识
目的 托管项目代码 基本概念 仓库Respository 存放项目代码,每个项目对应一个仓库,多个开源项目则有多个仓库. 收藏Star 收藏项目,方便下次查看. 复制克隆项目Fork ...
- SQL基础教程(第2版)第6章 函数、谓词、CASE表达式:练习题
END) AS low_price, END) AS mid_price, END) AS high_price FROM Product; 6_2.sql
- python 爬虫下载英语听力新闻(npr news)为mp3格式
想通过听实时新闻来提高英语听力,学了那么多年的英语,不能落下啊,不然白费背了那么多年的单词. npr news是美国国家公共电台,发音纯正,音频每日更新,以美国为主,世界新闻为辅,比如最近我国武汉发生 ...
- Thread--线程间通信--管道
在Java语言中提供了各种各样的输入/输出流Stream,使我们能够方便的对数据进行操作,其中管道流是一种特殊的流,用于在不同线程间直接传送数据.一个线程发送数据到输出管道,另一个线程从输入管道中读数 ...
- Linux-IO多路复用select函数实践
#include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/time.h ...
- CentOS7下MySQL8的二进制基本安装配置
前言 基于本地Centos7.6虚拟机Mysql8的配置(亲测有效) 一.安装前的准备 1.到官网下载mysql-8.0.16-linux-glibc2.12-x86_64.tar.xz 2.通过Xs ...
- Springboot注解--@Controller和@RestController的区别
1.使用@Controller 注解,在对应的方法上,视图解析器可以解析return 的jsp,html页面,并且跳转到相应页面:若返回json等内容到页面,则需要加@ResponseBody注解 2 ...