anon wrote:
> I'm new to sed and regular expressions, and I'm hoping
someone can
> explain to me why one regular expression works in ksh
and not
> another...
>
> I have a bunch of files containing CREATE TABLE sql
statements like so:
>
> CREATE TABLE db.table {
> KEY_COLUMN VARCHAR(20)
> ,KEY_COLUMN_02 CHAR(20)
> ,KEY_COLUMN_03 TIMESTAMP(6)
> ,NOT_A_KEY_COLUMN TIMESTAMP(6)
> ,CREATE_MODULE VARCHAR(1024)
> ..........
> };
>
> The format of this file is very well-defined. I want to
match any line
> with both:
>
> 1. Column name of 'KEY_COLUMN' (not 'xyz_KEY_COLUMN' or
> 'KEY_COLUMN_abc')
> 2. Type CHAR or VARCHAR (not TIMESTAMP or DECIMAL(9,2))
>
> My first attempt at a regular expression which matches
these lines was:
>
'<space>*,?KEY_COLUMN<space>*(VAR)?CHAR([0-9]*)'
In sed, you need (.....) instead of (.....) to group
multiple
characters, and use ? instead of ? to match zero/one
preceding
char/chars_group. try the pattern:
<space>*,?KEY_COLUMN<space>*(VAR)?CHAR([0-9]
*)
for example:
bash ~$ echo '
CREATE TABLE db.table {
KEY_COLUMN VARCHAR(20)
,KEY_COLUMN_02 CHAR(20)
,KEY_COLUMN_03 TIMESTAMP(6)
,NOT_A_KEY_COLUMN TIMESTAMP(6)
,CREATE_MODULE VARCHAR(1024)
..........
};
' | sed -n '/ *,?KEY_COLUMN *(VAR)?CHAR([0-9]*)/p'
KEY_COLUMN VARCHAR(20)
> The above expression does not work. What does work is:
>
'<space>*[<space>,]KEY_COLUMN<space>*[A-Z]
*CHAR[0-9]*)'
>
> The first one makes a lot more sense to me:
> '<space>*[<space>,]'
> seems like a confusing way of saying 'any number of
spaces followed by
> an optional comma', which is what I thought
'<space>*,?' meant
> and
>
> '[A-Z]*CHAR'
> does what I need it to do, but I still don't understand
what's wrong
> with '(VAR)?CHAR' As I said, I have an expression that
works, but I
> don't feel like I learned anything - I want to figure
out why what I
> had didn't work. Both failures did use question marks.
Any advice?
you may need to escape the question mark to make it a
metacharacter.
Good luck,
Xicheng
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regex googlegroups.com
To unsubscribe from this group, send email to
regex-unsubscribe googlegroups.com
For more options, visit this group at http://grou
ps-beta.google.com/group/regex
-~----------~----~----~----~------~----~------~--~---
|