C#:循环遍历字符串中的子字符串模式

ila*_*ann 2 c# regex string

我的模式如下:

{(code)}
其中代码是一个数字(最多6位数),或2个字母后跟一个数字.
例如:

{(45367)}
{(265367)}
{(EF127012)}

我想在长字符串中找到所有出现的内容,我不能只使用纯正则表达式,因为我需要在找到匹配项时执行某些操作(比如记录位置和匹配类型).

Lan*_*nce 5

您所指的内容仍然可以使用正则表达式完成.试试以下......

Regex regex = new Regex(@"\{\(([A-Z]{2}\d{1,6}|\d{1,6})\)\}");
String test = @"my pattern is the following:
Run Code Online (Sandbox Code Playgroud)

我想在长字符串中找到所有出现的内容

var matches = regex.Matches(test);
foreach (Match match in matches)
{
    MessageBox.Show(String.Format("\"{0}\" found at position {1}.", match.Value, match.Index));
}
Run Code Online (Sandbox Code Playgroud)

我希望有所帮助.