perl :^I;regexp;push/pop/正则集合

来源:互联网 发布:软件测试规范标准 编辑:程序博客网 时间:2024/06/05 18:29

this function is for add 3 space for each line between { }

#!/opt/exp/bin/perl -w

$^I=".bak";while (<>)  {if (/{/) {print $_;push @symbol, '}';}if (/}/) {print $_;pop @symbol;}if (!/{/ && !/}/)  {$number =@symbol;if ($number>0) {print "   $_";} else {print $_;}}}

$string =~ s/<.*?>//g;$string =~ s/<.*>//g;  不行perl -pe 's/<.*>//g' ucfirst.pl are also fine
第一个是非最贪心匹配!!!
对于这样的文本例子
abc<abccde>ddef>x
第一个将其转成
abcddef>x
第二个是
abcx
or,多选匹配
# 不管是“scarlet”“ruby”还是“puce”,一律换成“red
perl -pe 's/scarlet|ruby|puce/red/g' 8.txt
否定匹配
$temp="";while (<>) {        if (!/^$/) {                print $temp;        }         $temp=$_;}
区间范围的匹配# 显示两个正则表达式之间的文本(包含) perl -ne 'print if /Iowa/../Montana/'  正则掠过空白行和#comment行在指定范围行前加comments perl -pe 's/^/#/ if ($. >16 && $.<28)' 3-13.pl skip 空行和comments行 perl -ne 'print $_ unless (/^$/ ||/^#/);' filename# 显示包含65个以下字符的行 perl -nle 'print unless /.{65}/' # 删除文件中的所有空行(与“grep ‘.’ ”效果相同) perl -ne 'print if /./'  perl -pe 's/\n/ / if $.%2' 
正则错误部分
正则替换部分,不能有正则
$string =~ /-?(/d+)/.?(/d+)/; $string =~ s/-?(\d+)/.$1/;


0 0