开发者

convert comma-separated string-pairs with regex

I have a comma-separated list of first- and lastnames which I need to convert to SQL (whitespace exists aft开发者_运维问答er the comma):

joe, cool

alice, parker

etc.

should become:

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

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

How can I achieve this with a regular expression?


In Perl you can do this:

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

From the command line:

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

Input:

joe, cool
alice, parker

Output:

( firstname ='joe' and lastname = 'cool' )
( firstname ='alice' and lastname = 'parker' )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜