perl one-liner在正则表达式匹配上添加双引号

时间:2015-02-17 06:44:42

标签: regex perl

有没有办法想出一个perl one liner来执行以下操作? 为所有匹配

的字符串添加缺少的双引号"
"DNR for ABC-FULL*"

自:

(NULL,137,"DNR for ABC-FUL11002,576,"Crane, IN",207,NOW(),1,1),
(NULL,137,"DNR for ABC-FUL11003,577,"Crane, IN",208,NOW(),1,1),
(NULL,137,"DNR for ABC-FUL11004,578,"Crane, IN",209,NOW(),1,1),
(NULL,137,"DNR for ABC-FUL11005,579,"Crane, IN",210,NOW(),1,1),

要:

(NULL,137,"DNR for JRE-FUL11002,576","Crane, IN",207,NOW(),1,1),
(NULL,137,"DNR for JRE-FUL11003,577","Crane, IN",208,NOW(),1,1),
(NULL,137,"DNR for JRE-FUL11004,578","Crane, IN",209,NOW(),1,1),
(NULL,137,"DNR for JRE-FUL11005,579","Crane, IN",210,NOW(),1,1),

2 个答案:

答案 0 :(得分:1)

您需要使用捕获组或\K

$ perl -pe 's/(,"DNR for ABC-FUL[^"]*),/\1",/g' file
(NULL,137,"DNR for ABC-FUL11002,576","Crane, IN",207,NOW(),1,1),
(NULL,137,"DNR for ABC-FUL11003,577","Crane, IN",208,NOW(),1,1),
(NULL,137,"DNR for ABC-FUL11004,578","Crane, IN",209,NOW(),1,1),
(NULL,137,"DNR for ABC-FUL11005,579","Crane, IN",210,NOW(),1,1),

OR

$ perl -pe 's/,"DNR for ABC-FUL[^"]*\K,/",/g' file
(NULL,137,"DNR for ABC-FUL11002,576","Crane, IN",207,NOW(),1,1),
(NULL,137,"DNR for ABC-FUL11003,577","Crane, IN",208,NOW(),1,1),
(NULL,137,"DNR for ABC-FUL11004,578","Crane, IN",209,NOW(),1,1),
(NULL,137,"DNR for ABC-FUL11005,579","Crane, IN",210,NOW(),1,1),

答案 1 :(得分:0)

(?<="DNR for )[^-]*(-[^,]*)

试试这个。JRE$1"JRE\1"。请参阅演示。

https://www.regex101.com/r/rK5lU1/42

或      您可以使用perl的\K功能

"DNR for \K[^-]*(-[^,]*)

参见演示。

https://www.regex101.com/r/rK5lU1/43