简体   繁体   English

在每行的两个模式之间加上双引号

[英]Put in double quotes all between two patterns in every line

I have ndjson file, with strings like this: 我有ndjson文件,带有这样的字符串:

{"created": "2016-03-08 00:00:00 UTC", "changed": "2016-03-08 08:51:56 UTC", "rev": 28990, "status": 1, "user": [{"user_id": null, "name": null, "loyaltyCard": 123456789012}], "id": "26680533564", "tax": null, "products": [{"price": 289, "quantity": 1, "coupon": null, "id": "4122"}],  "shipping": 0.0}

I need to take in double quotes values of "loyaltyCard", considering, that it could be digits, letters (cyrillic also) and anything else. 考虑到可能是数字,字母(也包括西里尔字母)和其他任何东西,我需要对“ loyaltyCard”使用双引号。

Expecting to see something like: 期望看到类似:

UTC", "rev": 280, "status": 1, "user": [{"user_id": null, "name": null, "loyaltyCard": "123456789012"}], "id": "26680533564", "tax": null, "products": [{"price": 289, "quantity": 1, "coupon": null, "id": "4122"}],  "shipping": 0.0}
UTC", "rev": 56990, "status": 1, "user": [{"user_id": 543445, "name": null, "loyaltyCard": "1233bla456bla"}], "id": "5454580534", "tax": null, "products": [{"price": 869, "quantity": 2, "coupon": null, "id": "86854"}],  "shipping": 0.0}

If you want to wrap up loyaltyCard value in double quotes, you can use a regex like this: 如果您想将loyaltyCard值用双引号引起来,则可以使用如下正则表达式:

(loyaltyCard": )([^}]*)\}

With a replacement string: 用替换字符串:

\1"\2"}

Regex demo 正则表达式演示

Update for Vim: to find/replace in vim you can use: Vim 更新 :要在Vim中查找/替换,您可以使用:

:%s/\(loyaltyCard": \)\([^}]*\)\}/\1"\2"}/
 ^ ^----------------------------^ ^-----^
 | + Pattern1 (w/capture groups)  + Pattern 2 (w/ group refs)
 + substitute cmd

在此处输入图片说明

s/: (\d+)/: "$1"/ might do the trick

This is one of the way: 这是一种方式:

 m/"loyaltyCard"\:\s*"([^"]*)"/g;

If you want to give a escape sequence for quotes your wish. 如果您想给引号一个转义序列,您的愿望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM