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, @"\"([^\"]+)\"");
精彩评论