正则表达式

vim 正则

\\<'匹配的字符串'\\> 可以用来完全匹配
/s/\\t/    /g     替换所有tab字符为i空格

perl 温度转换

来自《精通正则表达式》

!#/bin/perl
print "Enter a temperature in Celsius:\n";
$celsius = <STDIN>; # get input
chomp($celsius);  # remove '\n'

if ( $celsius  =~ m/^[-+]?[1-9]+(\.[0-9]*)?$/){
        $fahrenheit = ($celsius * 9 /5) + 32; # get result; 
        #print "The anwser is \"$fahrenheit\".\n";
        printf "The fahrenheit is %.2f F when the celsius is %.2f C .\n", $fahrenheit,$celsius;
    }
else{
    print "Expecting a Number,so I don't understand\"$celsius\".\n"    
}

perl 生成标签

来自《精通正则表达式》


undef $/;
$text =<>;
$text =~ s/&/&amp;/g;
$text =~ s/</&lt/g;
$text =~ s/>/&gt/g;


#$text =~ s/^$/<p>/g;
#$text =~ s/^\s*$/<p>/mg;

$text =~ s{
    \b
    (
    \w[-.\w]*
    \@
    [a-z0-9]+(\.[-a-z0-9]+)*\.(com|edu|info)
    )
    \b
}{<a href="mailto:$1">$1</a>}gix;
print $text;

$text =~ s{
    \b
    (
    http://[-a-z0-9]+(\.[-a-z0-9]+)*\.(com|edu|info) \b
    (
        (?<![.,?!])
    )?
    )
}{<a href ="$1">$1</a>}gix;
print $text;

查找连续重复单词

$/=".\n";
while(<>){
    next if !s/\b([a-z]+)((?:\s|<[^>]+>)+)(\1\b)/\e[7m$1\e[m\e[7m$2\e[7m$3\e[m/ig;
    s/^(?:[^\e]*\n)+//mg;
    s/^/$ARGV: /mg;#删除所有未标记的行
    print;#在首行增加文件名

}

perl直接运行

perl -p -i -e "s/(\d)(?=(\d\d\d)+(?!=\d))/$1,/gi" test3.txt

posted @ 2018-12-04 17:56  诗酒  阅读(152)  评论(0编辑  收藏  举报