Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I'm not sure I've got this completed narrowed down, but please bear with me...
I've got a multi-line SQL SELECT statement generated by a tool where I want to extract the table name. Okay, easy enough. Here's simple code which plucks out the table name when there is a WHERE clause and when there is not:
#!/usr/bin/perl my $stmt0 = "SELECT table.field0, table.field1\nFROM table;\n"; my $stmt1 = "SELECT table.field0, table.field1\nFROM table\nWHERE fiel +d0 = 'foo';\n"; my $tablename = $1 if $stmt0 =~ m/^.*FROM\s+(\S+)\s*.*;$/s; print "table name =\"$tablename\"\n"; $tablename = ''; $tablename = $1 if $stmt0 =~ m/^.*FROM\s+(\S+)\s*.*;$/s; print "table name =\"$tablename\"\n";
I get the same answer either way, I would think that I've got a generalized regular expression which takes care of all types of SELECT statements.
Or so I thought...
When I run this regular expression to extract the table name in the script which accesses the tool, the tool emits a multi-line statement with no WHERE clause, and does not match anything with the above regular expression. If I take the semicolon out of the RE:
$tablename = $1 if $stmt =~ m/^.*FROM\s+(\S+)\s*.*$/gs;
I get the table name as table;. I can hack around this problem by adding a substitution afterwards:
$tablename = $1 if $stmt =~ m/^.*FROM\s+(\S+)\s*.*$/gs; $tablename =~ s/;//;
...but this only points out that I don't know what's going on.
I then dumped the statement to a file and wrote a small C program to read the string character-by-character to see what really was in it. In stepping through execution with a debugger, I saw nothing out of the ordinary -- the SELECT keyword, two fully-qualified field names, a 13,10 newline character pair given that this is on Windows followed by the FROM clause.
I'm baffled as to why I need the kludgy substitution. Any insight any of you can provide would be appreciated. Thanks.
20031203 Edit by Corion: Changed PRE tags to CODE tags
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: regular expression across lines?
by Roger (Parson) on Dec 02, 2003 at 23:50 UTC | |
by Anonymous Monk on Dec 03, 2003 at 00:02 UTC | |
by Roger (Parson) on Dec 03, 2003 at 00:22 UTC | |
by Anonymous Monk on Dec 03, 2003 at 00:14 UTC | |
|
Re: regular expression across lines?
by allolex (Curate) on Dec 03, 2003 at 08:11 UTC |