开发者

convert comma-separated string-pairs with regex

开发者 https://www.devze.com 2023-02-06 09:35 出处:网络
I have a comma-separated list of first- and lastnames which I need to convert to SQL (whitespace exists aft开发者_运维问答er the comma):

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

精彩评论

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