use strict; use warnings; my $stmt0 = "SELECT table.field0, table.field1\nFROM table0;\n"; my $stmt1 = "SELECT table.field0, table.field1\nFROM table1\nWHERE field0 = 'foo';\n"; my $nameA = $1 if $stmt0 =~ m/^FROM\s+(\S+)$/ms; my $nameB = $1 if $stmt1 =~ m/^FROM\s+(\S+)$/ms; my $name0 = do { $stmt0 =~ m/^FROM\s+(\w+)(;*?)$/ms; $1 }; my $name1 = do { $stmt1 =~ m/^FROM\s+(\w+)(;*?)$/ms; $1 }; print "table name A ='$nameA'\n"; print "table name B ='$nameB'\n"; print "table name 0 ='$name0'\n"; print "table name 1 ='$name1'\n"; #### table name A ='table0;' table name B ='table1' table name 0 ='table0' table name 1 ='table1'