c# - Detect the word after a regex -
i have long text , part of text is
hello , john how (1)are (are/is) you?
i used detect (1)
.
string optionpattern = "[\\(]+[0-9]+[\\)]"; regex reg = new regex(optionpattern);
but got stuck here @ continue on how detect after (1)
find are
.
full code ( falsetru bringing me far) :
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 split this, remove words after (0-9), when run it removes word after ()
in last detection.
as can see word after (7) gone rest not.
how detect are
after (1)
?
possible replace word after (1) textbox too?
use positive lookbehind lookup ((?<=\(\d+\))\w+
):
string text = "hello , john how (1)are (are/is) you?"; string optionpattern = @"(?<=\(\d+\))\w+"; regex reg = new regex(optionpattern); console.writeline(reg.match(text));
prints are
alternative: capture group (\w+)
string text = "hello , 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 @".."
, don't need escape \
.
update
instead of using .split()
, .replace()
:
string text = "hello , 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 , 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 , john how (1) (are/is) you?
Comments
Post a Comment