#!/usr/bin/env perl use strict; use warnings; my $ls = ''; my $str2 = "(schema1.table1(ratio:1.2-1.5)) union(column) (schema2.table2(ratio:1.3-1.6)) union(column) (schema3.table3(ratio:1.4-1.8)) "; my $re = ' ^ [\s\n]* # group 1 ( [(] ([\w]+) # schema name [.] ([\w]+) # table name (?: [(] ( ratio | unique ) # condition [:=] ([\d.]+) (?:-([\d.]+) )? [)] ) [)] [\n] ) (?: (?: ( union | intersect | except | minus ) ) [(] ([\w]+) [)] [\n] ( [(] ([\w]+) # schema name [.] ([\w]+) # table name (?: [(] ( ratio | unique ) # condition [:=] ([\d.]+) (?:-([\d.]+) )? [)] ) [)] [\n] ) # \g{1} )+ # [\n*]* $ '; for my $s ($str2) { if ($s =~ m/${re}/xsm) { for (my$i=0;$i>>", $s, "<<<\n"; } # captured output: -- 0: [124] [(schema1.table1(ratio:1.2-1.5)) union(column) (schema2.table2(ratio:1.3-1.6)) union(column) (schema3.table3(ratio:1.4-1.8)) ] -- 1: [32] [(schema1.table1(ratio:1.2-1.5)) ] -- 2: [8] [schema1] -- 3: [15] [table1] -- 4: [21] [ratio] -- 5: [25] [1.2] -- 6: [29] [1.5] -- 7: [83] [union] -- 8: [90] [column] -- 9: [124] [(schema3.table3(ratio:1.4-1.8)) ] -- 10: [100] [schema3] -- 11: [107] [table3] -- 12: [113] [ratio] -- 13: [117] [1.4] -- 14: [121] [1.8] # note: schema2 and its detail is missing