开发者

C# Regular Expression, Match contents of string

i am trying 开发者_StackOverflow中文版to capture the text in the inverted commas in the following string using c# regular expresions.

I've tried a whole lot of patterns but none of them are matching..

Can anyone help?

text1.text2 = text3["THISISWHATIWANTTOCAPTURE"].text4();


This one will do it:

(?<=\").*(?=\")

You can test the above regex here:

http://regexhero.net/tester/

In C#:

class Program
{
    static void Main(string[] args)
    {
        string pattern = "(?<=\").*(?=\")";
        string str =
        "text1.text2 = text3[\"THISISWHATIWANTTOCAPTURE\"].text4();";

        Match match = Regex.Match(str, pattern);

        foreach (var c in match.Captures)
        {
            Console.WriteLine(c);
        }
    }
}

Output:

THISISWHATIWANTTOCAPTURE


The simplest expression I'd come up with is

[a-zA-Z0-9]+\["(.+)"\]


try:

Match match = Regex.Match(string, @"\"([^\"]+)\"");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜