Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a line based upon the number of occurrences of a string

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; 
like image 931
user1939168 Avatar asked Nov 26 '25 16:11

user1939168


2 Answers

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'
        ];
like image 126
TLP Avatar answered Nov 28 '25 15:11

TLP


/(.*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

like image 45
creinig Avatar answered Nov 28 '25 17:11

creinig