正则表达式替换json文件中一个关键的价值 - regex to replace the value of a key in a json file
- 此内容更新于:2015-10-09
主题:
我想做一个正则表达式,所以我可以做一个“查找/替换”有许多对象的json文件。每个对象都有一个包含一个URL键命名为“资源”。看看这些例子:“资源”:“/设计/123/形象。jpg”“资源”:“/设计/221/大象。gif”“资源”:“/设计/图标。png”我想要一个正则表达式替换整个url字符串:localhost:8080/filepath。这种方法,结果将会是:“资源”:“localhost:8080/设计/123/形象。jpg”“资源”:“localhost:8080/设计/221/大象。gif”“资源”:“localhost:8080/设计/图标。png“我只是从正则表达式和我完全迷路了。我在想,一个有效的方法就是写一些这种模式从“资源”:“我怎么能写正则表达式?
原文:
I want to make a regex so I can do a "Search/Replace" over a json file with many object. Every object has a key named "resource" containing a URL.
Take a look at these examples:
"resource":"/designs/123/image.jpg"
"resource":"/designs/221/elephant.gif"
"resource":"/designs/icon.png"
I want to make a regex to replace the whole url with a string like this: localhost:8080/filepath.
This way, the result would be:
"resource":"localhost:8080/designs/123/image.jpg"
"resource":"localhost:8080/designs/221/elephant.gif"
"resource":"localhost:8080/designs/icon.png"
I'm just starting with regular expressions and I'm completely lost. I was thinking that one valid idea would be to write something starting with this pattern "resource":"
How could I write the regular expression?
网友:可能重复的Regex将JSON属性值在另一个字符串
(原文:Possible duplicate of Regex to wrap JSON property value inside another string)
解决方案:
最简单的方法可能是为了取代“资源”:“/“资源”:“localhost:8080/。你甚至不需要一个正则表达式(但如果你只是为了逃避一些东西)。
原文:
The easiest method is probably just to replace "resource":"/ with "resource":"localhost:8080/. You don't even need a regex for this (but if you do you just have to escape some stuff).
解决方案:
与vim这将是这应该是很容易转移到java。
原文:
With vim this would be
:%s/"resource":"\(.*\)"http://www.28im.com/"resource":"localhost:8080\1"this should be easily transferable to java.