A no-brainer regular expression

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, MudGuard

Post Reply
User avatar
talleyrand
Posts: 624
Joined: Mon Jul 21, 2003 6:56 pm
Location: Kansas City, MO, USA
Contact:

A no-brainer regular expression

Post by talleyrand »

Which ought to explain why I can't figure it out. I want to turn this

Code: Select all

    SELECT
    ,   company_name
    ,   company_DBA_name
    ,   preferred_comm_type
    ,   company_website
    FROM
       COMPANY C
into this

Code: Select all

    SELECT
    ,   @company_name = isnull(@company_name, company_name)
    ,   @company_DBA_name = isnull(@company_DBA_name, company_DBA_name)
    ,   @preferred_comm_type = isnull(@preferred_comm_type, preferred_comm_type)
    ,   @company_website = isnull(@company_website, company_website)
    FROM
       COMPANY C
The replacement expression I can handle. It'll look something like
\1@\2 = isnull\(@\2, \2\) but it's the search criteria that's kicking me in the bajingo.

Using POSIX style reg exp, I am attempting to find company_name, company_dba_name and the rest of the column names. I know syntactically the above SQL is invalid but I thought if the first line of it started with a comma, then I would know column names are in lines that look like , 3 spaces and then the column name. Column names will contain at least one letter and potentially underscores and digits. For the sake of brevity, the comma and spaces will be omitted in my following examples.
[[:alpha:]]+ will get the words to highlight. So I'm a third of the way there.
[[:alpha:]|[_]]+ This won't find anything
[[:alpha:]]|[_]+ This will find each piece of the word (excluding the digits) but not the whole word (which I'll need come replacement time)
[[:alpha:]]|[_]|[[:digit:]]+ This gets me everything but only one piece at a time.
Rereading the help on regular expressions I see that the or matches either the expression on the right or left so that led me to
[[:alpha:]]+|[_]+|[[:digit:]]+ which is back to highlighting the entire word, the underscore or the digit but not at the same time, only one element at a time. At this point I also chose to put the whitespace back in and saw that doing that wasn't quite what I wanted.
, [[:alpha:]]+|[_]+|[[:digit:]]+ starts off great --- it grabs the comma the space and first word, then it catches either the underscore or the digits or the next instance of comma 3xSpace word. I think I need to be able to tell it to group the OR'd stuff together but I'm just not seeing how to do it.

What am I missing? Besides the time to sit and read the Mastering Regular Expressions book? I've hacked together a macro that works better than manually doing it but a solution would make me feel better. Not as good as having seen it myself but better than nothing.

Thanks.
I choose to fight with a sack of angry cats.
User avatar
s_reynisson
Posts: 939
Joined: Tue May 06, 2003 1:59 pm

Post by s_reynisson »

Using POSIX, is there anything else? 8)
Looks like you just made a typo to me.

Find (^ *, *)([^ ]+) *$
Replace \1@\2 = isnull(@\2, \2)

1. (^ *, *) : Start of line, any number of spaces, a comma, and some
more spaces if you like.
2. ([^ ]+) : Anything that is not a space and at least one character.
3. *$ : Any number of spaces at the end of line, but no () so who cares.
ie. you could just use Find (^ *, *)([^ ]+).
Then I open up and see
the person fumbling here is me
a different way to be
Post Reply