打印处理文件,后面跟着id
本文关键字:id 处理 文件 打印 | 更新日期: 2023-09-27 18:13:01
private void button1_Click(object sender, EventArgs e)
{
string value = textBox1.Text;
string[] lines = value.Split(Environment.NewLine.ToCharArray());
foreach (string l in lines)
if (l.IndexOf("Processing")==0)
{
textBox4.Text = textBox4.Text + l + Environment.NewLine;
}
MatchCollection matches2 = Regex.Matches(value, "'"([^'"]*)'"");
foreach (Match match2 in matches2)
{
foreach (Capture capture2 in match2.Captures)
{
textBox4.Text = textBox4.Text + Environment.NewLine + capture2.Value;
}
}
}
MY Input is:-
Processing ''Users''bhargava''Desktop'New.txt
<"dfgsgfsgfsgfsgfsgfsgfs">
Processing ''Users''bhargava''Desktop''New.txt
<"dfgsgfsgfsgfsgfsgfsgfs">
Processing ''Users''bhargava''Desktop'New.txt
Processing ''Users''bhargava''Desktop''New.txt
I Need Output:-
Processing ''Users''bhargava''Desktop'New.txt
"dfgsgfsgfsgfsgfsgfsgfs"
Processing ''Users''bhargava''Desktop''New.txt
"dfgsgfsgfsgfsgfsgfsgfs"
Processing ''Users''bhargava''Desktop'New.txt
Processing ''Users''bhargava''Desktop''New.txt
I Am getting:-
Processing ''Users''bhargava''Desktop'New.txt
Processing ''Users''bhargava''Desktop''New.txt
Processing ''Users''bhargava''Desktop'New.txt
Processing ''Users''bhargava''Desktop''New.txt
"dfgsgfsgfsgfsgfsgfsgfs"
"dfgsgfsgfsgfsgfsgfsgfs"
请帮帮我!
您的代码中缺少一对花括号。第一个foreach需要花括号来封装后面的所有代码。如果没有它,它只是抓取下一个语句并对行中的每个元素重复它。这就是为什么您得到的每一行都以"Processing"开头,后面跟着您的ID的两个匹配项。试试这个:
private void button1_Click(object sender, EventArgs e)
{
string[] lines = value.Split(Environment.NewLine.ToCharArray());
foreach (string l in lines)
{
if (l.IndexOf("Processing")==0)
{
textBox4.Text = textBox4.Text + l + Environment.NewLine;
}
MatchCollection matches2 = Regex.Matches(l, "'"([^'"]*)'"");
foreach (Match match2 in matches2)
{
foreach (Capture capture2 in match2.Captures)
{
textBox4.Text = textBox4.Text + Environment.NewLine + capture2.Value;
}
}
}
}
所以在这个例子中,我添加了我提到的花括号,我也去掉了value
,而是告诉正则表达式搜索当前行。如果您每次都搜索value,那么最终得到的ID重复次数将远远超过您想要的输出。