简体   繁体   English

使用正则表达式从文件中提取JSON键值映射

[英]Extract JSON key-value map with a regex from a file

I'm not really strong in regexes and tried to solve a problem with a parser written by hands, my own bicycle, which failed on some inputs unpredicted by me though, the problem is as follows: I have JavaScript i18n files that, along with translations, contain some other configuration stuff that may be defined somewhere in a file (that's the main reason why it's pretty hard to handle the problem with hand made parser), so the file is something like that: 我的正则表达式能力不是很强,尝试用我自己的自行车手写的解析器解决问题,但是由于某些我无法预测的输入而失败,问题如下:我有JavaScript i18n文件,该文件以及翻译包含可能在文件中某个位置定义的其他一些配置内容(这是手工解析器很难解决该问题的主要原因),因此文件类似:

(function() {
    'use strict';
    //some configuration stuff (some other stuff may be insterted)
    var translations = angular.module('module.translations.languages.enUs', []);

    translations.constant('translationsName', {
     "first_label":"first_label_value",
     "second_label":"second_label_value"
     //etc
});

}());

The example above is only one of possible template options, but they all have one thing in common - translation labels are defined as a key-value json which is nothing but a java map serialized to json. 上面的示例只是可能的模板选项之一,但是它们都有一个共同点-转换标签被定义为键值json,它不过是序列化为json的java映射。 My goal is to get only these key-value json from a file, deserialize it to map, do some operations with it and insert it back again. 我的目标是从文件中仅获取这些键值json,将其反序列化为映射,对其进行一些操作,然后再次插入。 So the question is: perhaps someone has ready and proven regex that could handle this kind of situation - find a map of key-value json in a text? 因此,问题是:也许有人已经准备好了,并且已经过验证的正则表达式可以处理这种情况-在文本中找到键值json的映射? If so, I would be really grateful for that! 如果是这样,我将非常感谢! Thanks, Cheers, Andrey 谢谢,干杯,安德烈

You could use this regexp to find "key":"value" pairs : 您可以使用此正则表达式查找"key":"value"对:

"([^"]+)"\\s*:\\s*"([^"]+)",?

Group 1 is the key, Group 2 is the value 组1是关键, 组2是价值

It will also find "key": "value" , "key" :"value" or "key" : "value" pairs. 它还将找到"key": "value""key" :"value""key" : "value"对。

Demo on regexplanet (click the Java button then click Test button) regexplanet上的演示 (单击Java按钮,然后单击“ 测试”按钮)

Also a demo on regex101 也是regex101上的演示

Explanation 说明

"([^"]+)" : Capture any character but a double-quote between double-quotes (this is the key ) "([^"]+)" :捕获除双引号之间的双引号以外的任何字符(这是

\\s*? : Followed by zero or more whitespace :后跟零个或多个空格

\\s* : Followed by a colon \\s* :后跟冒号

\\s* : Followed by zero or more whitespace \\s* :后跟零个或多个空格

"([^"]+)" : Capture any character but a double-quote between double-quotes (this is the value ) "([^"]+)" :捕获除双引号之间的双引号以外的任何字符(这是

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

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