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?
dkim18Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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.
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
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

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

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

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

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

!whitespace.Contains(input[current]))
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

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

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

Open in new window

dkim18Author Commented:
Thanks guys.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.