# Sql script with regex

**URL:** <https://www.ruby-forum.com/t/sql-script-with-regex/174674>\
**Category:** Ruby\
**Created:** [August 31, 2009, 11:16am UTC](https://www.ruby-forum.com/t/sql-script-with-regex/174674 "2009-08-31T11:16:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [August 31, 2009, 11:16am UTC](https://www.ruby-forum.com/t/sql-script-with-regex/174674/1 "2009-08-31T11:16:59Z")

</div>

"INSERT INTO CBM\_CT\_BY\_CLT ( "  
" CST\_CD, "  
" BLK\_REG\_SEQ, "  
" BLK\_REG\_DT, "  
" DEL\_FLG "  
") "  
"VALUES ( "…

how can I get the table name(CBM\_CT\_BY\_CLT) and column name ( CST\_CD  
…) from this sql script?  
It contains spaces and the script is line by line.  
I am trying like this but it is no useless  
columnname = text.scan(/"\s+(.\*?)\s+"\/)  
please help…

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [August 31, 2009, 12:05pm UTC](https://www.ruby-forum.com/t/sql-script-with-regex/174674/2 "2009-08-31T12:05:43Z")

</div>

Ahmet K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> It contains spaces and the script is line by line.  
> I am trying like this but it is no useless  
> columnname = text.scan(/“\s+(.\*?)\s+”\/)

You will have to write a SQL parser.

Of course, you may try to do a half-assed job with regular  
expressions, but SQL is a language with a context-free grammar, so you  
need a parser.

---

<div class="post-metadata">

**Author:** ![ahmet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@ahmet](https://www.ruby-forum.com/u/ahmet)\
**Post date:** [September 1, 2009, 5:59am UTC](https://www.ruby-forum.com/t/sql-script-with-regex/174674/3 "2009-09-01T05:59:59Z")

</div>

Pascal J. Bourguignon wrote:

> Ahmet K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> > It contains spaces and the script is line by line.  
> > I am trying like this but it is no useless  
> > columnname = text.scan(/“\s+(.\*?)\s+”\/)
> 
> You will have to write a SQL parser.
> 
> Of course, you may try to do a half-assed job with regular  
> expressions, but SQL is a language with a context-free grammar, so you  
> need a parser.

I think SQL parser is very deep for me.  
I got it with regex. Thank you very much.
