c# - regex to find a string that comes after = [duplicate]

<div class="question-status question-originals-of-duplicate">

This question already has an answer here:

    <li> regex to find a string that comes after = <span class="question-originals-answer-count"> 6 answers </span> </li> </ul>

    So I asked this question before and said I wanted it in javascript but realized later on that it's unecessary data being sent. So it would be great if anybody could help me solve the same thing in C#

    What I need is to get several properties out of a string.

    The string will look something like:

    str = "car[brand=saab][wheels=4]";

    There can be more or fewer properties.

    I need everything before the first [] in 1 variable.

    Then I need each property and its value in a variable.

    Easiest way to understand what I want is probably to check my previous question and the answer that solved it :)

    Answer1:

    I used the regex(slightly different) in your previous question.

    string input = "car[brand=saab][wheels=4]"; string product = ""; Dictionary<string, string> props = new Dictionary<string, string>(); foreach (Match m in Regex.Matches(input, @"^(\w+)|\[(\w+)=(.+?)\]")) { if (String.IsNullOrEmpty(product)) product = m.Groups[1].Value; else props.Add(m.Groups[2].Value, m.Groups[3].Value); }

    Answer2:

    try this regex:

    (.+?)(\[.+?\])+

    and a sample code:

    var inputString = "car[brand=saab][wheels=4]"; var pattern = @"(?<v1>.+?)(?<v2>\[.+?\])+"; var v1 = Regex.Match(inputString, pattern).Groups["v1"].Value; Dictionary<String, String> list = new Dictionary<String, String>(); foreach (Capture capture in Regex.Match(inputString, pattern).Groups["v2"].Captures) { var sp = capture.Value.Split('='); list.Add(sp[0], sp[1]); }

    explain:

    <strong>(?<name>subexpression)</strong> Captures the matched subexpression into a named group.

    Answer3:

    You can do this

    var lst=Regex.Matches(input,@"(\w+)((?:\[.*?\])+)") .Cast<Match>() .Select(x=>new { name=x.Groups[1].Value, value=Regex.Matches(x.Groups[2].Value,@"(?<=\[).*?(?=\])") .Cast<Match>() .Select(x=>new { name=x.Groups[0].Value.Split('=')[0], value=x.Groups[0].Value.Split('=')[1] }) }); <hr>

    Now you can iterate over lst like this

    foreach(var parent in lst) { parent.name;//car foreach(var pairs in parent.value) { pairs.name;//brand,wheels pairs.value;//ferrari,4 } } <hr>

    So,for input car[brand=a][wheels=4]cycle[brand=b][wheels=2]

    Output would be like

    car brand,a wheels,4 cycle brand,b wheels,2

    Answer4:

    Without regex:

    string input = "car[brand=saab][wheels=4]"; var query = from s in input.Replace("]", "").Split('[') let vars = s.Split('=') let name = vars[0] let value = vars.Length > 1 ? vars[1] : "" select new {Name = name, Value = value}; string firstVar = query.First().Name; Dictionary<string, string> otherVars = query .Skip(1) .ToDictionary(v => v.Name, v => v.Value);

    You can access your variables in the dictionary like this string brand = otherVars["brand"]

    Answer5:

    Since you already have an answer using regex and your comments state it doesn't have to be with a regex, I'll offer an alternative:

    The code is

    string str = ("car[brand=saab][wheels=4]"); int i = str.IndexOf("["); string[] details =str.Substring(i).Replace("]","").Split('['); string name = str.Substring(0, i); string brand = details[1].Split('=')[1]; string wheels = details[2].Split('=')[1];

    This approach assumes the data is always going to be in the same format though; you may need some validation in there depending on your needs...

    人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:c# - regex to find a string that comes after = [duplicate]