in reply to Simple little regex to build a list

Assuming valid aliases must match \w+, you can say

my @aliases = $sql =~ /\bas\s+(\w+)/ig;
The /i is necessary because SQL is generally case-insensitive. Note that this may return false positives if, eg, your query has a string literal with the word "as" in it, but it's a first approximation. Global (/g) match in list context returns the list of patterns matching the group (\w+). perlretut goes into this also.

Also, I don't know if this is standard or not, but some databases allow the keyword "as" to be omitted, so you can say

SELECT foo a, bar b FROM baz

Replies are listed 'Best First'.
Re^2: Simple little regex to build a list
by wolis (Scribe) on Feb 14, 2005 at 05:19 UTC
    Ah.. That is the little bit of perl wisdom I was missing:
    my @aliases = $sql =~ /\bas\s+(\w+)/ig;
    I didn't know I could build up a list from the result of a regex.. but I sort of hoped I could.

    Its the little ways things can be combined that makes perl so much fun.

    The good news for all you worried monks is that I am only looking at the tables section of the SQL (I break my SQLs into $tables, $field, $where and $order)

    The other good news is that I alway use 'as' in this particular application.

    So good news all around!

    Thanks monks.

    PS I try not to use modules as I mostly work on virtual hosts and I dont have that much control.

    ___ /\__\ Creative Object World COW \/__/ www.wolispace.com/cow "What is the world coming to?"