tags:

views:

75

answers:

3

Hi, i am trying 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();
A: 

try:

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

The simplest expression I'd come up with is

[a-zA-Z0-9]+\["(.+)"\]
Jim Brissom
+2  A: 

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
Leniel Macaferi
Has multiple captured groups. OP wants to capture only `THISISWHATIWANTTOCAPTURE`
Michael Petrotta
Michael, no more capturing groups...
Leniel Macaferi