Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

c# - Getting values between quotes

How can I get the value between quotes with an RegEx

for example I want to find all the parameters from the function test

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you just want the args to test, you'll need to include that 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 am using quote detection here.

Alternatively - if you just want the 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 the quotes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...