perl正则典型应用

#!/usr/bin/perl
use strict;
use warnings;

my $sql="select name,age from user where id=1";

#匹配
print '$sql是一个查询语句!'."\n" if $sql =~ m/^select\s+([\w|,|\*]+)\s+from\s+(\w+)/gi;

#不匹配
print '$sql不是xml文本!'."\n" if $sql !~ m/<\s*(\w+)\s*>/gi;

#替换
print "替换结果:$sql\n" if $sql =~ s/user/USER/;

#转化
print "转化结果:$sql\n" if $sql =~ tr/a-z/A-Z/;

#提取
print "表名:$2,查询列:$1\n" if $sql =~ m/^select\s+([\w|,|\*]+)\s+from\s+(\w+)/gi;

#拆分
map {print "$_\n"} split(/\s/,$sql);

编程技巧