• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

searching string from a text file?

Hi,

I am trying find a string (it will be an URI) from a text file containing lines of URIs.
basically each line has an URI.

I was trying to use this Regex method but I am getting the unescape character problem.
 readFile = new System.IO.StreamReader("StatusLog.txt");
            string allRead = readFile.ReadToEnd();
            readFile.Close();

            //If the match is found in allRead
           
            if (Regex.IsMatch(allRead, fileName)
            {
                return true;
            }
            else
            {              
                return false;
            }


Is there a way to fix this problem?
I am looking at the Regex.Escape() but not sure how to use it.

Is there another way I can search the string?
Should I do while loop and readline(), then compare?
Can't think a better way to do this?
0
dkim18
Asked:
dkim18
  • 5
  • 5
  • 2
2 Solutions
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hello, have you tried:
if (allRead.IndexOf(fileName, StringComparison.InvariantCultureIgnoreCase) >= 0)
{
    return true;
}
else
{               
    return false;
}

Open in new window

0
 
käµfm³d 👽Commented:
I agree with the above--I think a simple string search would be better for this purpose than would a regex. Unless you have some variance in your filenames, I can can't see any benefit to your using a regex.

I am looking at the Regex.Escape() but not sure how to use it.
Regex.Escape just parses the string you give it and escapes (i.e. adds a leading backslash to) any characters which have special meaning in regex-land. The return value of that function is the escaped string.

Should I do while loop and readline(), then compare?
If you have a really big file, then ReadLine would probably be more preferable; if your files are small, then ReadAll should be adequate.
0
 
dkim18Author Commented:
if (allRead.IndexOf(fileName, StringComparison.InvariantCultureIgnoreCase) >= 0)
{
    return true;
}
else
{              
    return false;
}

My file has these:
http://mysite.com/website1/
http://mysite.com/website2/
http://mysite.com/website2/subfolers/
http://mysite.com/website2/subfolers2/

if I was looking for http://mysite.com/website2/
Would  not the indexOf() bring back three result?
I need to exact match and bring back one result from above?


I
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
dkim18Author Commented:
readFile = new System.IO.StreamReader("StatusLog.txt");
            string allRead = readFile.ReadToEnd();
            readFile.Close();

            //If the match is found in allRead
           
            if (Regex.IsMatch(allRead, fileName)
            {
                return true;
            }
            else
            {              
                return false;
            }
readFile = new System.IO.StreamReader("StatusLog.txt");
            
          while ((line = readFile.ReadLine()) != null){
              
               if (line ==filename)
              {   return true; break;
               }
             }
             return fase;


something like this?

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hello, based in the kaufmed sugestion, I think is better in this way:
using (System.IO.StreamReader sr = new System.IO.StreamReader("StatusLog.txt"))
{
    string myLine = sr.ReadLine();
    while (myLine != null)
    {
        if (myLine.IndexOf(fileName, StringComparison.InvariantCultureIgnoreCase) >= 0)
        {
            return true;
        }
        myLine = sr.ReadLine();
    }
    return false;
}

Open in new window

0
 
käµfm³d 👽Commented:
Would  not the indexOf() bring back three result?
If you only call IndexOf once, then you will only get the first occurrence. If you want all occurrences, then you'll need to repeatedly call the method (i.e. loop). IndexOf is overloaded to take a start index as the second parameter. This is how you can skip the result(s) you just matched. For example:

using System.Collections.Generic;
using System.Linq;

namespace _27379331
{
    class Program
    {
        static void Main(string[] args)
        {
            System.IO.StreamReader readFile = new System.IO.StreamReader("StatusLog.txt");
            string find = "http://mysite.com/website2/";
            string input = readFile.ReadToEnd();
            int start = 0;
            List<string> matches = new List<string>();

            while ((start = input.IndexOf(find, start)) > 0)
            {
                string whitespace = "\n\r\t";
                int current = start;
                
                while (current < input.Length && !whitespace.Contains(input[current]))
                {
                    current++;
                }

                matches.Add(input.Substring(start, current - start));
                start = current;
            }
        }
    }
}

Open in new window


For what you are after, the whole URL, I have included a bit of logic to read, once the base URL is found, up to the next whitespace. This should return you the whole URL.
0
 
käµfm³d 👽Commented:
Hmmm...  I thought I had a space in my "whitespace" list. In case EE stripped it out, the whitespace variable should have a space in that list. I'll try again:

string whitespace = " \n\r\t";

Open in new window

0
 
dkim18Author Commented:
Sorry, how do you convert char to sring here?

!whitespace.Contains(input[current]))
0
 
käµfm³d 👽Commented:
Sorry, how do you convert char to sring here?
Ah. I had Linq enabled, so I got a few extension methods! You can call the ToString method:

...!whitespace.Contains(input[current].ToString()))

Open in new window

0
 
dkim18Author Commented:
never mind.
 !whitespace.Contains(allRead[current].ToString()))
0
 
käµfm³d 👽Commented:
You could also change that line to:

while (current < input.Length && whitespace.IndexOf(input[current]) < 0)

Open in new window

0
 
dkim18Author Commented:
Thanks guys.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 5
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now