正则表达式使用行号在双引号和单引号内获取文本。
例如:
line 123 Processing File "This is what i am looking for"<'this is also what i need'>
输出应为:
line 123 This is what i am looking for this is also what i need
我的正则表达式是:
MatchCollection matches2 = Regex.Matches(l, "\"([^\"]*)\"");
foreach (Match match2 in matches2)
{
foreach (Capture capture2 in match2.Captures)
{
textBox4.Text = textBox4.Text + capture2.Value + Environment.NewLine;
}
}
我得到了(我的输出):
"This is what i am looking for"
我不需要双引号,我只需要引号内的文字。
答案 0 :(得分:1)
这里的第一个问题是你正在看比赛而不是捕捉。捕获将显示()运算符收集的内容。
matches2.OfType<Match>().Select(x => x.Captures[0].Value)
答案 1 :(得分:0)
试试这个:
Regex.Replace(inputtext, @"line (\d+).*?\""(.*?)""\W+(.*?)'", "line $1 $2 $3")
答案 2 :(得分:0)
string input =
"line 123 Processing File \"This is what i am looking for\"<'this is also what i need'>";
string output =
string.Join(" ", Regex.Matches(input, @"(line\s+(\d+)|(""|').*?\3)")
.Cast<Match>()
.Select(m => Regex.Replace(m.Value,
@"^[""']|[""']$", "")));
输出:
line 123 This is what i am looking for this is also what i need