Retrieving values โ€‹โ€‹between quotes

How can I get the value between quotes using RegEx

for example i want to find all parameters from a function test

<html>
   test("bla");
   print("foo");
   test("moo");
</html>

      

The result should be {"bla", "moo"}

+1


source to share


2 answers


If you just want args test

, you need to include this in the regex:

    StringBuilder sb = new StringBuilder("{");
    bool first = true;
    foreach (Match match in Regex.Matches(html, @"test\((""[^\""]*\"")\)"))
    {
        if(first) {first = false;}
        else {sb.Append(',');}
        sb.Append(match.Groups[1].Value);
    }
    sb.Append('}');
    Console.WriteLine(sb);

      

From the question, I'm using the quote definition here.



Alternatively - if you only want values:

    foreach (Match match in Regex.Matches(html, @"test\(""([^\""]*)\""\)"))
    {
        Console.WriteLine(match.Groups[1].Value);
    }

      

The main change here is that the group is now inside quotes.

+2


source


edit: Removed old code and created linq version ...



    var array = (from Match m in Regex.Matches(inText, "\"\\w+?\"")
                 select m.Groups[0].Value).ToArray();

    string json = string.Format("{{{0}}}", string.Join(",", array));

      

+1


source







All Articles