3

perl 文字列変数に sql クエリがあるとします。

select a from table one union select b from table two union select c from table three union select d from table four union select e from table five union select f from table six union select g from table seven union select h from table eight

上記のテキストには、ユニオンで区切られた 8 つの個別のクエリがあります。

それらの半分をある変数に保存し、残りの半分を別の変数に保存したい。

私は、常に 8 つのクエリがあり、その間に 7 つのユニオンがあることを知っています。次のスクリプトを試しましたが、機能していません。

#!/usr/bin/perl

use strict;
use warnings;

my $var="select a from table one union select b from table two union select c from table three union select d from table four union select e from table five union select f from table six union select g from table seven union select h from table eight";
$var=~m/(.*union{3})(.*)/g;

print $1; 
4

2 に答える 2

4

split先読みアサーションの文字列はいつでも使用できます。\b単語境界アサーションを使用して部分一致を回避します。

use strict;
use warnings;
use Data::Dumper;

my $str = "select a from table one union select b from table two union select c from table three union select d from table four union select e from table five union select f from table six union select g from table seven union select h from table eight";

my @query = split /(?=\bunion\b)/i, $str;
print Dumper \@query;

出力:

$VAR1 = [
          'select a from table one ',
          'union select b from table two ',
          'union select c from table three ',
          'union select d from table four ',
          'union select e from table five ',
          'union select f from table six ',
          'union select g from table seven ',
          'union select h from table eight'
        ];
于 2013-03-18T13:49:11.757 に答える
1
/(.*union{3})(.*)/

可能な限り多くの文字 (".*") に一致し、その後にリテラル "unio" が続き、その後にちょうど 3 つの "n" 文字が続き、その後に任意の数の文字が続きます。おそらく次のようなものが必要です。

/^((.*?union){3})(.*)$/

つまり (できるだけ少ない文字で、その後に "union" が続きます)、3 回、その後に何かが続きます。

これは、クエリの「前半」、1 つの部分、および残りのクエリの 3 つのグループに一致します。したがって、あなたの場合、グループ $1 と $3 に興味があります

于 2013-03-18T13:52:21.467 に答える