Regex 替换 JSON 文件中键的值

regex to replace the value of a key in a json file

提问人:shivakumar 提问时间:10/8/2015 更新时间:10/8/2015 访问量:846

问:

我想制作一个正则表达式,这样我就可以在具有许多对象的 json 文件上进行“搜索/替换”。每个对象都有一个名为“resource”的键,其中包含一个 URL。

请看这些示例:

“资源”:“/designs/123/image.jpg”

“资源”:“/designs/221/elephant.gif”

“资源”:“/设计/图标.png”

我想制作一个正则表达式来将整个 url 替换为如下字符串:localhost:8080/filepath。

这样,结果将是:

“资源”:“localhost:8080/designs/123/image.jpg”

“资源”:“localhost:8080/designs/221/elephant.gif”

“资源”:“localhost:8080/designs/icon.png”

我刚开始使用正则表达式,我完全迷失了。我在想一个有效的想法是写一些从这个模式“资源”开始的东西:”

如何编写正则表达式?

Java 正则表达式

答:

1赞 Astrogat 10/8/2015 #1

最简单的方法可能是将“resource”:“/替换为”resource“:”localhost:8080/。你甚至不需要正则表达式(但如果你这样做了,你只需要转义一些东西)。

-1赞 AlexN 10/8/2015 #2

使用 vim 时,这将是

:%s/"resource":"\(.*\)"/"resource":"localhost:8080\1"

这应该可以很容易地转移到 Java 上。