[英]How to parse string with regex with [i] inside string
我有这样的表达
R403[i] == 3 and R404[i] == 2 AND R405[i] == 1 and R403[i+1] == 5 and R404[i+1] == 2 AND R405[i+1] == 1 R231[2]
我想使用此正则表达式使表达式全部出现
[R|M|B].*?\]
但是要再次应用的一个条件必须在其中包含变量i
,这样R121[1]
就不会被捕获。
有人可以帮忙吗?
如果我正确理解您的问题,这将是正则表达式:
Regex reg = new Regex(@"[RMB]\d{3}\[.*?i.*?\]");
如果您的“ i”较少的表达式可以在带有i的表达式之前出现:
Regex reg = new Regex(@"[RMB]\d{3}\[[^]]*?i[^]]*?\]");
试试这个解决方案。 它将检查模式的组匹配。
string pattern = @"[RMB]\d{3}\[\d*\]";
string input = " M403[50] == 3 and R404[i] == 2 AND R405[i] == 1 and R403[i+1] == 5 and R404[i+1] == 2 AND R405[i+1] == 1 R231[2]";
foreach (Match match in matches)
{
Console.WriteLine("Mactch: {0}", match.Value);
Console.WriteLine();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.