[英]Regex: Match repeated groups with non repeated groups
I have a Json string that I need to extract some data from using Regex in C# The string is something like this: 我有一个Json字符串,我需要从C#中使用Regex提取一些数据。该字符串是这样的:
{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": [
{
"arrayKey1": 1,
"arrayKey2": "something",
"arrayKey3": "somethingelse"
},
{
"arrayKey1": 2,
"arrayKey2": "something2",
"arrayKey3": "somethingelse2"
},
{
"arrayKey1": 3,
"arrayKey2": "something3",
"arrayKey3": "somethingelse3"
}
],
"some very long text here": ""
"anotherKey": "value",
"keylast": "valuelast"
}
I want to extract the array's values with named groups, I did it with the following regex: (?:"arrayKey1": (?<arrayKey1>[^"]+),[\\n\\t ]+"arrayKey2": "(?<arrayKey2>[^"]+)",[\\n\\t ]+"arrayKey3": "(?<arrayKey3>[^"]+)")
我想使用命名的组提取数组的值,我使用以下正则表达式完成了它
(?:"arrayKey1": (?<arrayKey1>[^"]+),[\\n\\t ]+"arrayKey2": "(?<arrayKey2>[^"]+)",[\\n\\t ]+"arrayKey3": "(?<arrayKey3>[^"]+)")
This works great and I get each match for each item of the array with 3 groups of each key. 这很好用,我得到每个匹配项,每个匹配项都有3组每个键。
Now I want to add an extra match that will contain only the value of "anotherKey" I can't get to work, Here are some regexs I have tried but didn't work: 现在,我想添加一个额外的匹配项,该匹配项仅包含我无法使用的“ anotherKey”的值,这是我尝试过但无法使用的一些正则表达式:
(?:"arrayKey1": (?<arrayKey1>[^"]+),[\n\t ]+"arrayKey2": "(?<arrayKey2>[^"]+)",[\n\t ]+"arrayKey3": "(?<arrayKey3>[^"]+)")(?:[\s\S]*)(?:"anotherKey": "(?<anotherKey>[^"]+)")
This one does get the "anotherKey", but it only returns the first item in the array, not all of them. 这个确实获得了“ anotherKey”,但是它只返回数组中的第一项,而不是全部。
Also: https://regex101.com/r/mfXlRs/1 另外: https : //regex101.com/r/mfXlRs/1
Can someone put me in the right way? 有人可以把我摆放正确吗?
Thanks 谢谢
Your regex for arrayKey1..3
(only) gave three separate matches, and in each match you got the 3 required values. 您的
arrayKey1..3
正则表达式(仅)给出了三个单独的匹配项,在每个匹配项中,您都获得了3个必需值。
Now, after you added the fragment looking for anotherKey
, but at the parent level, the situation changed. 现在,在添加片段以寻找
anotherKey
,但在父级上,情况发生了变化。 Now you have only a single match, because: 现在你只有一个单一的比赛,这是因为:
arraykeys
. arraykeys
。 (?:[\\s\\S]*)
matches everything up to anotherKey
, including both remaining sets of arraykeys
. (?:[\\s\\S]*)
将所有内容匹配到anotherKey
,包括剩下的两组arraykeys
。 anotherKey
. anotherKey
。 Maybe you should perform your matching in 2 separate steps: 也许您应该通过2个单独的步骤执行匹配:
anotherKey
. anotherKey
。 Adding +
quantifier to the "old" group will not help, because if a capturing group was matched several times, then the group would hold only the last match. 将
+
量词添加到“旧”组将无济于事,因为如果捕获组被匹配多次,则该组将仅保留最后一个匹配项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.