脚本中用到的测试文本内容
cat ./test.txt
Helo, This is a test file.
This file contain a story about fred flintstone and his friend.
One day fred went to shoping, he met Sam who is his friend.
Sam was also for shopping. So they got together.
lalalal fred or wilma.
When they on their way home, they met frederick and Alfred.
Frederick and Alfred want to look after their granpa Mr. Slate.
this line just test Fred
and this line FRED
add this line Bamm-Bamm
wilma and fred
aa
abcdefhijk
foot
goooood
不使用命名捕获脚本为:运行结果为:
fred Sam
使用命名捕获脚本为:运行结果为:
fred Sam
cat ./test.txt
Helo, This is a test file.
This file contain a story about fred flintstone and his friend.
One day fred went to shoping, he met Sam who is his friend.
Sam was also for shopping. So they got together.
lalalal fred or wilma.
When they on their way home, they met frederick and Alfred.
Frederick and Alfred want to look after their granpa Mr. Slate.
this line just test Fred
and this line FRED
add this line Bamm-Bamm
wilma and fred
aa
abcdefhijk
foot
goooood
不使用命名捕获脚本为:
- #! /usr/bin/perl
- if (! open TEST,"./test.txt") {
- die $!;
- }
- while () {
- if (/(fred\b).*(sam\b)/i) {
- print "$1 $2\n";
- }
- }
fred Sam
使用命名捕获脚本为:
- #! /usr/bin/perl
- if (! open TEST,"./test.txt") {
- die $!;
- }
- while () {
- if (/(?fred\b).*(?sam\b)/i) {
- print "$+{name1} $+{name2}\n";
- }
- }
fred Sam
编辑回复