简体   繁体   English

从查询字符串中提取变量

[英]Extract variables from querystring

I need to parse the following string 我需要解析以下字符串

/MyData.csv(MemberCount.sum,Production.sum[Salesperson="James Almond","Area="Europe "Area 1" (Germany)",Area="North America",Area="North America [Level A]"])

The first part is easy, however the clause within the brackets ([]) is giving me a bit of headache since it can contain double quotes as the example shows. 第一部分很容易,但是方括号([])中的子句让我有些头疼,因为如示例所示,它可能包含双引号。 (note the content within the brackets can change dynamically) (请注意方括号内的内容可以动态更改)

I expect the following output when parsing the last part of the string : 解析字符串的最后一部分时,我期望以下输出:

Salesperson James Almond Area Europe "Area 1" (Germany) Area North America Area North America [Level A] 销售人员James Almond地区欧洲“地区1”(德国)地区北美地区北美[A级]

I've been working with regex but can't seem to get it right. 我一直在使用正则表达式,但似乎无法正确处理。 Hoping someone have the magic! 希望有人拥有魔力!

You can use the fact that the 'true' closing double quotes are either followed by a comma or the closing square bracket: 您可以使用以下事实:'true'右双引号后跟逗号或右方括号:

Area="(?<Area>.*?)"(?=\]|,)

regex101 demo . regex101演示

You may give a try to the Balancing Group Definitions : 您可以尝试一下“ 平衡组定义”

string pattern = @"^[^\[\]]*" +
                @"(" +
                @"((?'Open'\[)[^\[\]]*)+" +
                @"((?'Close-Open'\])[^\[\]]*)+" +
                @")*" +
                @"(?(Open)(?!))$";

var results =
    Regex.Match(input, pattern)
    .Groups["Close"].Value
    .Split(new char[] { '=', ',' });

This outputs: 输出:

Salesperson
"James Almond"
Area
"Europe "Area 1" (Germany)"
Area
"North America"
Area
"North America [Level A]"

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

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