Let's say I have a sql query in a perl string variable:
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
In the text above I have eight separate queries separated by a union.
I want half of them stored in one variable and other half in another variable.
I know that there are supposed to be eight queries all the time and 7 unions in between. I tried the following script but is is not working.
#!/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;
You can always split the string on a lookahead assertion. Using the \b word boundary assertions to avoid partial matches.
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;
Output:
$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'
];
/(.*union{3})(.*)/
matches as many characters as possible (".*"), followed by the literal "unio", followed by exactly 3 "n" characters, followed by any number of characters. You probably want something like this:
/^((.*?union){3})(.*)$/
i.e. (as few characters as possible, followed by "union"), three times, followed by anything.
This matches three groups -- the "first half" of the query, one single part and the remaining query. So in your case you'll be interested in groups $1 and $3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With