Link to home
Start Free TrialLog in
Avatar of dkim18
dkim18

asked on

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?
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Hello, have you tried:
if (allRead.IndexOf(fileName, StringComparison.InvariantCultureIgnoreCase) >= 0)
{
    return true;
}
else
{               
    return false;
}

Open in new window

Avatar of kaufmed
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.
Avatar of dkim18
dkim18

ASKER

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
Avatar of dkim18

ASKER

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

SOLUTION
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of dkim18

ASKER

Sorry, how do you convert char to sring here?

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

Avatar of dkim18

ASKER

never mind.
 !whitespace.Contains(allRead[current].ToString()))
You could also change that line to:

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

Open in new window

Avatar of dkim18

ASKER

Thanks guys.