JSON

正则表达式替换json文件中一个关键的价值 - regex to replace the value of a key

字号+ 作者:H5之家 来源:H5之家 2015-10-15 18:04 我要评论( )

28im技术大牛是面向程序员的知识学习与分享社区,追求最前沿技术

正则表达式替换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.

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 正则表达式优化JSON字符串的技巧

    正则表达式优化JSON字符串的技巧

    2016-01-18 09:06

  • SQLite 3.9支持JSON、表达式索引及更多特性

    SQLite 3.9支持JSON、表达式索引及更多特性

    2015-11-10 16:47

网友点评