[英]Extracting Number from a Text File in C# Windows Form
我已经编写了使用Windows From从文本文件中提取数字的代码。 问题是,输出是部分发生的。 第一行是“打印”或最后一行。 我想要所有包含数字的行
(i.e) If the text file contains,
Auto 2017
Mech 2056
CSE 2016
我只希望打印那些2017、2056、2016。
这是代码:
private void button1_Click(object sender, EventArgs e)
{
string infile = textBox1.Text;
StreamReader sr = new StreamReader(infile);
string allDetails = File.ReadAllText(infile);
string result = Regex.Match(allDetails, @"\d+").Value;
richTextBox1.Text = result.ToString();
}
您正在尝试获取数值。 Regex.Matches将帮助您解决问题。
下面是简化的代码。
private void button1_Click(object sender, EventArgs e)
{
string filedetails = File.ReadAllText(textBox1.Text);
var regexCollection = Regex.Matches(filedetails, @"\d+");
foreach (Match rc in regexCollection)
richTextBox1.AppendText(rc.Value + ",");
}
如果要输出2017,2056,2016
,可以使用这种方法
private void button1_Click(object sender, EventArgs e)
{
string infile = textBox1.Text;
string[] lines = System.IO.File.ReadAllLines(infile);
string temp = "";
int i = 0;
foreach (string line in lines)
{
string result = Regex.Match(line, @"\d+").Value;
if (i == 0)
{
temp = result;
}
else
{
temp = temp + "," + result;
}
i++;
}
richTextBox1.Text = temp;
}
或者如果您想要单一值2017
2056
2016
则
private void button1_Click(object sender, EventArgs e)
{
string infile = textBox1.Text;
string[] lines = System.IO.File.ReadAllLines(infile);
foreach (string line in lines)
{
string result = Regex.Match(line, @"\d+").Value;
richTextBox1.Text = result ;
}
}
您需要使用该方法。 Regex.Matches
。 Matches
方法在指定的输入字符串中搜索所有出现的正则表达式。
Match
方法仅返回第一个匹配项,需要检索后续匹配项。
private void button1_Click(object sender, EventArgs e)
{
string infile = textBox1.Text;
StreamReader sr = new StreamReader(infile);
string allDetails = File.ReadAllText(infile);
var regexMatchCollection = Regex.Matches(allDetails, @"\d+");
foreach(Match mc in regexMatchCollection)
{
richTextBox1.AppendText(mc.Value);
richTextBox1.AppendText(",");
}
}
不使用正则Regex
,下面是简化代码
private void button1_Click(object sender, EventArgs e)
{
StringBuilder numbers = new StringBuilder();
string allDetails = File.ReadAllText(textBox1.Text);
foreach(string word in allDetails.Split(' '))
{
int number;
if(int.TryParse(word, out number))
{
numbers.Append(number);
numbers.Append(",");
}
}
richTextBox1.Text = numbers.Trim(',');
}
test.txt有
Auto 2017
Mech 2056
CSE 2016
在下面的程序File.ReadAllLines
中将每一行分别存储在一个string array
我们将使用foreach loop
读取一行,并将提取的数字存储在列表中(例如2017年),最后存储在string.Join
加入后,我们将加入该数组并用","
分隔每个单词并将其保存在字符串中
List<string> list = new List<string>();
var textfile = File.ReadAllLines(@"D:\test.txt");
foreach (var line in textfile)
{
string result = Regex.Match(line, @"\d+").Value;
list.Add(result);
}
string numbers = string.Join(",",list.ToArray());
输出值将是
2017,2056,2016
private void button1_Click(object sender, EventArgs e)
{
string infile = textBox1.Text;
StreamReader sr = new StreamReader(infile);
string allDetails = File.ReadAllText(infile);
string result = string.Empty;
foreach (var item in Regex.Matches(allDetails, @"\d+"))
{
result = result + item.ToString() + ",";
}
richTextBox1.Text = result.TrimEnd(',');
}
static void Main(string[] args)
{
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\admin\Desktop\ConsoleApplication1\ConsoleApplication1\txtFile.txt");
List<string> Codelst = new List<string>();
foreach (var item in lines)
{
var a= Regex.Match(item, @"\d+").Value;
Codelst .Add(a);
}
var r = Codelst;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.