[英]Detect the word after a regex
I have a long text and part of the text is 我有一个很长的文字,部分文字是
Hello , i am John how (1)are (are/is) you? 你好,我是约翰你(1)是(你是)吗?
I used this to detect (1)
. 我用它来检测(1)
。
string optionPattern = "[\\(]+[0-9]+[\\)]";
Regex reg = new Regex(optionPattern);
But I got stuck here at continue on how to detect after (1)
to find are
. 但我来到这里停留在继续就如何检测后, (1)
发现are
。
Full code ( thanks to falsetru for bringing me this far) : 完整代码(感谢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);
}
I assume if I split like this, it will remove all the words after (0-9), however when I run it it only removes the word after ()
in the last detection. 我假设如果我这样拆分,它将删除(0-9)之后的所有单词,但是当我运行它时它只删除最后一次检测中的()
后面的单词。
As you can see the word after (7) is gone but the rest is not. 你可以看到(7)之后的单词已经消失,但其余的则没有。
How do I detect the are
after the (1)
? 如何检测的are
后(1)
Is it possible to replace the word after (1) with a textbox too? 是否可以用文本框替换(1)之后的单词?
Use positive lookbehind lookup ( (?<=\\(\\d+\\))\\w+
): 使用正向lookbehind查找( (?<=\\(\\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));
prints are
打印are
Alternative: capture a group (\\w+)
替代方案:捕获一组(\\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]);
BTW, using @".."
, you don't need to escape \\
. 顺便说一句,使用@".."
,你不需要逃避\\
。
UPDATE UPDATE
Instead of using .Split()
, just .Replace()
: 而不是使用.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, ""));
alternative: 替代方案:
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"));
prints 版画
Hello , i am John how (1) (are/is) you?
Would something like this work? 会这样的吗?
\((?<number>[0-9]+)\)(?<word>\w+)
Groups already added for ease of use. 已添加组以方便使用。 :) :)
Try this, 试试这个,
string text = "Hello , i am John how (1)are (are/is) you?";
string optionPattern = "[\\(]+[0-9]+[\\)]";
Regex reg = new Regex(optionPattern);
Match t = reg.Match(text);
int totallength = t.Index + t.Length;
string final = text.Substring(totallength,text.length-totallength);
in string final remaining text after (1) will store. 在(1)之后的字符串最后剩余文本将存储。
If you want to replace the text (I'm assuming that you are looking for some HTML), try: 如果你想替换文本(我假设你正在寻找一些HTML),试试:
var input = "Hello , i am John how (1)are (are/is) you?";
var output= Regex.Replace(input, @"(?<=\(\d*\))\w*", m => {
return "<input type='text'/>";
});
And this is how the output is being rendered: http://jsfiddle.net/dUHeJ/ . 这就是输出的呈现方式: http : //jsfiddle.net/dUHeJ/ 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.