正则单行和多行处理

前端之家收集整理的这篇文章主要介绍了正则单行和多行处理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
m 将待匹配串视为多行,^符号匹配字符串的起始或新的一行的起始;$符号匹配任意行的末尾。

以下例只匹配第一行为a,否则无匹配;

sh-3.2$ cat a6.pl 
$line='a
b
c';
if ($line =~ /^(.*)$/m){print $&."\n"};
sh-3.2$ perl a6.pl 
a

当成多行处理,只能匹配到a

$& ---匹配的内容


s 将待匹配串视为单行。可以匹配\n

sh-3.2$ cat a6.pl 
$line='a
b
c';
if ($line =~ /^(.*)$/s){print $&."\n"};
sh-3.2$ perl a6.pl 
a
b
c

-----------------------------------------------------------------------------------------------------------

/s  把多行当成单行处理

sh-3.2$ cat a5.pl
#!/usr/bin/perl   
use strict;
use DBI;
my $dbName = 'orcl';  
my $dbUser = 'test';  
my $dbUserPass = 'test';  
my $dbh = DBI->connect("dbi:Oracle:$dbName",$dbUser,$dbUserPass) or die "can't connect to database ";
if ($#ARGV <0){
   print "请输入一个参数\n";
  exit(-1);
}
my $var=$ARGV[0];
my $sql = $var;

if ($var =~ /select\s+(.+)\s+from\s+.*/is){ $a = $1};
chomp ($a);
my @arr = (split /,/,"$a");
foreach (@arr){
chomp ($_);
print $_." ";
}
my $sth = $dbh->prepare($sql); 
$sth->execute();  
while (my  @arr = $sth->fetchrow_array()){
print "\n@arr\n";
}
sh-3.2$ perl a5.pl "select a.session_id,a.sql_id,a.blocking_session,a.sample_time,a.module,a.PROGRAM,a.event,b.sql_TEXT
  from v\$active_session_history a,v\$sqlarea b
 where a.sql_id = b.sql_id" | head -10 
a.session_id 
       a.sql_id 
       a.blocking_session 
       a.sample_time 
       a.module 
       a.PROGRAM 
       a.event 
       b.sql_TEXT
  
1635 1bnt1kmfb8y30  05-JAN-15 11.38.04.823 AM sql*Plus sqlplus@jhoa (TNS V1-V3)  select * from dba_objects order by 1,2,3,4,5
原文链接:https://www.f2er.com/regex/360883.html

猜你在找的正则表达式相关文章