简体   繁体   English

如何使用正则表达式解析类似JSON的文件

[英]How to parse JSON-like file with regex


I have this structure of my input data, it is just like JSON but not containing strings. 我的输入数据具有这种结构,就像JSON一样,但不包含字符串。 I only need to parse few information from these data 我只需要解析这些数据中的一些信息

{ .appVersion = "1230"; DisplayStrings = ( A ); customParameters = ( { name = Axes;.......(continues)}'''

the code looks like this, what happens here is that it matches but search until last appearance of semicolon. 代码看起来像这样,这里发生的是它匹配但搜索到最后一个分号。 I tried all non-greedy tips and tricks that I have found, but I feel helpless. 我尝试了所有发现的非贪婪技巧和窍门,但我感到无助。

const regex = /.appVersion = (".*"?);/
const found = data.match(regex)
console.log(found)

How can I access value saved under .appVersion variable, please? 请问如何访问保存在.appVersion变量下的值?

You need to escape the . 您需要逃脱. before appVersion since it is a special character in Regex and you can use \\d instead of .* to match only digits. appVersion之前,因为它是Regex中的特殊字符,您可以使用\\d而不是.*来仅匹配数字。 If you want just the number to be captured, without the quotes you can take them out of the parentheses. 如果只想捕获数字,而没有引号,则可以将其从括号中删除。

const regex = /\.appVersion = "(\d+)";/
const found = data.match(regex)
const appVersion = found[1];
const string = '{ .appVersion = "1230"; DisplayStrings = (...(continues)';
const appVersion = string.match(/\.appVersion\s*=\s*"([^"]+)"/)[1];

If that's what you need... 如果那是您所需要的...

I'm not sure where the format you're trying to parse comes from, but consider asking (making) your data provider return json string, so you could easily invoke JSON.parse() which works in both node and browser environments. 我不确定您要解析的格式来自哪里,但是可以考虑让(让)数据提供者返回json字符串,因此您可以轻松地调用可在节点和浏览器环境中使用的JSON.parse()

You can try the following: 您可以尝试以下方法:

 var data='{ .appVersion = "1230"; DisplayStrings = ( A ); customParameters = ( { name = Axes;.......(continues)}'; const regex = /.appVersion = [^;]*/ //regex test: https://regex101.com/r/urX53f/1 const found = data.match(regex); var trim = found.toString().replace(/"/g,''); // remove the "" if necessary console.log(found.toString()); console.log(trim); 

Your regex is looking for . 您的正则表达式正在寻找. which is "any character" in a regex. 这是正则表达式中的“任何字符”。 Escape it with a backslash: 用反斜杠转义:

/\.appVersion = ("\d+");/

Don't use .* to capture the value, It's greedy. 不要使用.*来获取值,这是贪婪的。

You can use something like \\"[^\\"]* - Match a quote, then Any character except quote, as many time as possible. 您可以使用\\"[^\\"]* -尽可能匹配引号,然后匹配除引号之外的任何字符。

try 尝试

const regex = \.appVersion = \"([^\"]*)\";

Note that the first dot is should also be quoted, and the spaces should be exactly as in your example. 请注意,第一个点也应加引号,并且空格应与示例中的完全相同。

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

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