我有一个很长的文字,部分文字是
你好,我是约翰你(1)是(你是)吗?
我用它来检测(1).
string optionPattern = "[\\(]+[0-9]+[\\)]";
Regex reg = new Regex(optionPattern);
Run Code Online (Sandbox Code Playgroud)
但我被困在这里继续如何检测后(1)发现are.
完整代码(感谢falsetru为我带来这么远):
string optionPattern = @"(?<=\(\d+\))\w+";
Regex reg = new Regex(optionPattern);
string[] passage = reg.Split(lstQuestion.QuestionContent);
foreach (string s in passage)
{
TextBlock tblock = new TextBlock();
tblock.FontSize = 19;
tblock.Text = s;
tblock.TextWrapping = TextWrapping.WrapWithOverflow;
wrapPanel1.Children.Add(tblock);
}
Run Code Online (Sandbox Code Playgroud)
我假设如果我这样拆分,它将删除(0-9)之后的所有单词,但是当我运行它时它只删除()在最后一次检测之后的单词.

你可以看到(7)之后的单词已经消失,但其余的则没有.
我如何检测are后(1)?
是否可以用文本框替换(1)之后的单词?
fal*_*tru 19
使用正向lookbehind lookup((?<=\(\d+\))\w+):
string text = "Hello , i am John how (1)are (are/is) you?";
string optionPattern = @"(?<=\(\d+\))\w+";
Regex reg = new Regex(optionPattern);
Console.WriteLine(reg.Match(text));
Run Code Online (Sandbox Code Playgroud)
版画 are
替代方案:捕获一个组 (\w+)
string text = "Hello , i am John how (1)are (are/is) you?";
string optionPattern = @"\(\d+\)(\w+)";
Regex reg = new Regex(optionPattern);
Console.WriteLine(reg.Match(text).Groups[1]);
Run Code Online (Sandbox Code Playgroud)
顺便说一句,使用@"..",你不需要逃避\.
UPDATE
而不是使用.Split(),只是.Replace():
string text = "Hello , i am John how (1)are (are/is) you?";
string optionPattern = @"(?<=\(\d+\))\s*\w+";
Regex reg = new Regex(optionPattern);
Console.WriteLine(reg.Replace(text, ""));
Run Code Online (Sandbox Code Playgroud)
替代方案:
string text = "Hello , i am John how (1)are (are/is) you?";
string optionPattern = @"(\(\d+\))\s*\w+";
Regex reg = new Regex(optionPattern);
Console.WriteLine(reg.Replace(text, @"$1"));
Run Code Online (Sandbox Code Playgroud)
版画
Hello , i am John how (1) (are/is) you?
Run Code Online (Sandbox Code Playgroud)