. 任意字符
?0或者1个
* 任意个
+ 一个或者以上
$_ 默认数组
$@ 第一被匹配的字符
$` 被匹配字符之前的字符
$' 被匹配字符之后的字符
$1 第一个被匹配的字符,以左括号的顺序算。
<>砖石输入符
=~ 匹配判断符号
\d 数字
\D 非数字
\w [A-Za-z0-9_]
\W 非 [A-Za-z0-9_]
\s 字符
\S 非字符
{n} 重复n次
open FILE, "file.txt" 打开已经存在的文件
open FILE,">file.txt" 打开file.txt,如果不存在的话就创建file.txt
open FILE,">>file.txt" 打开,并将新内容追加到文本的末端,如果不存在的话,创建file.txt
双引号内的转义符
\n 换行
\r 回车
\t 制表符
\f formfeed
\b 退格
\a 响铃
\e escape(ASCII 中的escape 字符)
\007 任何八进制值(这里是,007=bell(响铃))
\x7f 任何十六进制值(这里是,007=bell)
\cC 一个控制符(这里是,ctrl +c)
\\ 反斜线
\” 双引号
\l 下个字符小写
\L 接着的字符均小写直到\E
\u 下个字符大写
\U 接着的字符均大写直到\E
\Q 在non-word 字符前加上\,直到\E
\E 结束\L,\U 和\Q
example1
从一个mail.list中识别 @eric.com的usr name,排序后输出到result.list中。
mail.list:
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
perl script:
代码
#
! /usr/bin/perl #perl directory declaration
open
MAIL
,
"
mail.list
"
;
#
open and read mail.list context
open
RESULT
,
"
>result.list
"
;
#
create a new file named" result.list", using filehandle "RESULT" to transfer information
$n
=
0
;
#
define a varibale
foreach
(
<
MAIL
>
){
#
processing each line from context <MAIL>
if
(
/
(
\
@eric
\.
com)
/
)
#
judge if pattern"@eric.com" matched, and store it in "$&"
{
$array
[
$n
]
=
"
$`\n
"
;
#
store words before matched("$`") in @array
$n
=
$n
+
1
;
#
index add one
}
}
@sorted
=
sort
(
@array
);
#
sort array by letters
print
RESULT
@sorted
;
#
print array in output file
close
RESULT;
#
close file
close
MAIL;
result.list
a
b
c
d
example2
将当前目录下所有 .cc结尾的文件 重命名为 .c结尾
#
! /usr/bin/perl
@list
=
glob
(
'
./*.cc
'
);
foreach
$list
(
@list
){
my
$name
=
$list
;
$name
=~
s
/
cc$
/
c
/
;
rename
$list
,
$name
;
}
example 3
将文件的中的各个module 实例化,输出到新的文件中。
代码
#
! /usr/bin/perl
open
TMP
,
"
>instance.v
"
;
while
(
<>
){
if
(
/^
module (
.*
)
\
((
.*
)
\
);$
/
m){
$module_name
=
$
1
;
$port_list
=
$
2
;
@ports
=
split
(
/,/,
$port_list
);
my
$n
=
@ports
;
my
$i
=
0
;
print
TMP
"
$module_name U_$module_name(\n
"
;
for
(
$i
=
0
;
$i
<
$n
;
$i
=
$i
+
1
){
print
TMP
"
$ports[$i]($ports[$i]),\n
"
;
if
(
$i
==
$n
-
1
){
print
TMP
"
);\n
"
;
}
}
}
}
close
TMP;
close
CODE;
windows下perl脚本范例
1
#
! C:\strawberry\perl\bin\perl
2
system
(
"
update.bat
"
); #运行脚本
3
open
FILE
,
"
file.lst
"
;
4
@lines
=<
FILE
>
;#将文本所有内容读入@lines
5
6
foreach
$lines
(
@lines
){ #处理@lines中的每行
7
chomp
(
$lines
);
8
open
SOURCE
,
"
$lines
"
;
9
my
@content
=<
SOURCE
>
;
10
open
Result
,
"
> ./result/$lines
"
; #windows下的路径也是用斜杠,而不是反斜杠
11
#
$lines =~ s/txt/png/;
12
#print Result "$lines\n";
13
foreach
$content
(
@content
)
14
{
15
if
(
$content
=~
/
Image filename
/
)
16
{
17
chomp
(
$content
);
18
@dirname
=
split
(
/
"
/,$content);
19
print Result
"
$dirname
[$
#
dirname] \n";
20
print
"
$dirname[$#dirname] \n
"
;
21
}
22
23
24
if
(
$content
=~
/
(Objects with ground truth
:
)
/
)
25
{
26
$re
=
$
'
;
27
@num =split(/\s/,$re);
28
print Result "$num[0] \n";
29
}
30
31
if($content =~ /(\(Xmin, Ymin\) - \(Xmax, Ymax\) :)/)
32
{
33
$a=$
'
;
34
if
(
$a
=~
/
([
0
-
9
]
+
)
\
D
*
([
0
-
9
]
+
)
\
D
*
([
0
-
9
]
+
)
\
D
*
([
0
-
9
]
+
)
/
)
35
{
36
print
Result
"
$1 $2 $3 $4 \n
"
;
37
}
38
39
}
40
41
}
42
close
SOURCE;
43
close
Result;
44
45
}
46
close
FILE;
47
48