Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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());
}

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
Posted

This is fairly simple, so you don't need to use regular expressions. Something like this would be easier:

C#
string[] lines = File.ReadAllLines(filePath);

IEnumerable<string> selectLines = lines.Where(line => line.EndsWith("23/10/2010"));

foreach (var item in selectLines)
{
    Console.WriteLine(item);
}
 
Share this answer
 
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

C#
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
C#
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);
    }
}
 
Share this answer
 
v3
Comments
2gizzo 26-Oct-10 3:44am    
Brilliant! This worked perfectly thank you very much.
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.



C#
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());
        }
    }
}




 
Share this answer
 
I composed lines myself but you should read from your file
C#
string[] lines = {"236,Janet, Stones,26300,19/10/2010",
                             "203,Linda,Phiri,15000,23/10/2010",
                             "150,Harry,Banda,16700,19/10/2010",
                             "418,Bridget, Tatianna,16/09/2010",
                             "210,Joseph,Alonzo,2900,19/10/2010"};
           string date = @"19/10/2010";
           string pattern = @"((\w|\s)*,){3,4}"+date;
           foreach(string str in lines)
           {
               Console.WriteLine(Regex.Match(str,pattern ).Value.ToString());
           }
 
Share this answer
 
Comments
2gizzo 26-Oct-10 4:08am    
Once again I appreciate the effort this worked perfectly so now i dont know which to use between the two solutions but thanks alot!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900