从文本文件中提取特定行 [英] Extract specific lines from text file
问题描述
我有一个结构化的文本文件,其中包含客户信息,这里有一些示例数据
236,Janet,Stones,26300,19/10/2010
203,Linda,Phiri,15000,23/10/2010
150,Harry,Banda,16700,09/08/2010
418,Bridget,Tatianna,16/09/2010
210,约瑟夫,阿隆佐,2900,23/10/2010
我希望从此文本文件中提取包含特定日期的行,例如,我想提取所有包含"23/10/2010"的行
所以我写了下面的代码
I have a structured text file which contains customer information here iz a few sample data
236,Janet, Stones,26300,19/10/2010
203,Linda,Phiri,15000,23/10/2010
150,Harry,Banda,16700,09/08/2010
418,Bridget, Tatianna,16/09/2010
210,Joseph,Alonzo,2900,23/10/2010
from this text file i wish to extract lines which contain a specific date so for example I want to extract all the lines containing ''23/10/2010''
so i wrote the following code
static void main(string[] args)
{
string path = @"C:\customers.txt";
StringBuilder buffer = new StringBuilder();
using (StreamReader = new StreamReader(path)){
while(sr.Peek() >=0){
if(Regex.IsMatch(sr.Readline(), "23/10/2010"))
buffer.Append(sr.ReadLine());
}
}
Console.WriteLine(buffer.ToString());
}
当我运行它而不是得到两行时,我只得到一个,更奇怪的是它仅输出第二行而不是第一行,所以在这种情况下,我的结果是210,Joseph,Alonzo,2900,23/10/2010和如果一行中只有1个发生在23/10/2010,那么我得到的结果是空的,所以我真的需要这个帮助!感谢
when i run this instead of getting two lines i get only one whats even more strange is it only output the second line and not the first line so my result in this case is 210,Joseph,Alonzo,2900,23/10/2010 and if only 1 occurence had 23/10/2010 in the line then im getting empty results so I really need help with this one! Thanks
推荐答案
这非常简单,因此您不需要使用正则表达式.这样的事情会更容易:
This is fairly simple, so you don''t need to use regular expressions. Something like this would be easier:
string[] lines = File.ReadAllLines(filePath);
IEnumerable<string> selectLines = lines.Where(line => line.EndsWith("23/10/2010"));
foreach (var item in selectLines)
{
Console.WriteLine(item);
}
您可以使用
字符串.包含链接 [链接 [
You could use
String.Contains link[^]
or
String.IndexOf link[^] - IndexOf should return >= 0 for the line to contain the string you are searching for
instead of Regex matching, simple and straight-forward
string stringToSearch = @"<your_string>";
string[] lines = File.ReadAllLines(@"path_to_your_file");
foreach (string line in lines)
{
if (line.Contains(stringToSearch))
{
Console.WriteLine(line);
}
}
或
or
string stringToSearch = @"<your_string>";
string[] lines = File.ReadAllLines(@"path_to_your_file");
foreach (string line in lines)
{
if (line.IndexOf(stringToSearch) >= 0)
{
Console.WriteLine(line);
}
}
我没有建议完成任务的其他方法,而是着重介绍了为什么您的代码无法正常工作.
我使用的是C ++,而不是C#,但是通过调试代码来查看问题出在哪里,这很简单.这些更改是微不足道的,在此我将不予赘述.仅将工作代码粘贴到此处.
为了分隔输出中的行,我尝试了C转义序列"\ r"和"\ n",但我想C#使用了一些不同的方法.
Instead of suggesting alternate ways of accomplishing the task I focussed on why your code isn''t working.
I work with C++, not C# but its a simple matter to see whats wrong by debugging the code. The changes are trivial and I won''t mention them here; just the working code is pasted here.
To separate lines in the output I tried the C escape sequence "\r" and "\n" but I guess C# uses some different method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string path = @"h:\customers.txt";
StringBuilder buffer = new StringBuilder();
using (StreamReader sr = new StreamReader(path))
{
while(sr.Peek() >=0) {
String str = sr.ReadLine();
if(Regex.IsMatch(str, "23/10/2010"))
buffer.Append(str + @"\n");
}
}
Console.WriteLine(buffer.ToString());
}
}
}
这篇关于从文本文件中提取特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!