根据我收集的信息,您正在尝试匹配以下列表。如果您可以更明确地尝试匹配(更多规则等),那么为其制作正则表达式应该相当容易。
- 从打印开始
- 后面有 1+ 个非单词字符
- 有一个报价
- 之后有东西(任何东西)
- 有一个报价
- 行结束
你可以试试这个:
var pattern = @"^PRINT[^\w]+""(.*)""$"; // you usually need those [] things :)
// * ""$ - requires the " to be at the end of the line
// * .* should match an empty quote ""
// you should trim the string on this one before matching
这是似乎表明它有效的测试代码:
// notice that one of these has an embedded quote
var list = new [] { "PRINT", "PRINT ", "PRINT \"STUFF\"", "PRINT\t \t\"AND \"FUN \"", " PRINT \"BAD\" " };
var pattern = @"^PRINT[^\w]+""(.*)""$";
foreach(var value in list) {
var m = Regex.Match(value, pattern);
if (m.Success) {
Console.WriteLine("success: '{0}' found '{1}'", value, m.Groups[1].Value);
} else {
Console.WriteLine("failed: '{0}'", value);
}
}
结果:
failed: 'PRINT'
failed: 'PRINT '
success: 'PRINT "STUFF"' found 'STUFF'
success: 'PRINT "AND "FUN "' found 'AND "FUN '
failed: ' PRINT "BAD" '