gpt4 book ai didi

regex - 如何使用正则表达式替换子字符串值?

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:33 28 4
gpt4 key购买 nike

例如,我在一个文件中有几行,每一行都是一个字符串,如下所示:

{"a":"53124647,44398061,44398058","b":"hello","c":"ha"}
{"b":"99","a":"44398049,44398031,44398028,10007072315,9569405558,10007072318","c":"ha"}
...

如果将每一行视为一个json对象,则每行中键"a"的值长度是灵活的,那么如何使用sed正则表达式将键"a"及其值对替换为"a":"10"

最佳答案

$ echo '{"a":"53124647,44398061,44398058","b":"hello","c":"ha"}
{"b":"99","a":"44398049,44398031,44398028,10007072315,9569405558,10007072318","c":"ha"}' | sed 's/"a":"[^"]*"/"a":"10"/g'
{"a":"10","b":"hello","c":"ha"}
{"b":"99","a":"10","c":"ha"}

关于regex - 如何使用正则表达式替换子字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36640115/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com