2011-01-25 455 views
0

我有一個逗號分隔的第一代和lastnames名單,我需要轉換爲SQL (逗號之後存在空格):轉換逗號分隔字符串對正則表達式與

joe, cool 

alice, parker 

應該變成:

(firstname ='joe' and lastname = 'cool') or 

(firstname ='alice' and lastname = 'parker') 

我如何使用正則表達式實現這一目標?

+0

哪種語言? – codaddict 2011-01-25 11:49:33

+0

我會在任何運行在linux上的編輯器中手動執行它:vi,kate等 – trajectory 2011-01-25 11:59:24

回答

1

在Perl中,你可以這樣做:

s/(\S+),\s*(\S+)/(firstname ='\1' and lastname = '\2')/ 

在命令行:

> perl -pe "s/(\S+),\s*(\S+)/(firstname ='\1' and lastname = '\2')/" input.txt 

輸入:

joe, cool 
alice, parker 

輸出:

(firstname ='joe' and lastname = 'cool') 
(firstname ='alice' and lastname = 'parker') 
相關問題