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("St atusLog.tx t");
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?
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("St
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?
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.
ASKER
if (allRead.IndexOf(fileName, StringComparison.Invariant CultureIgn oreCase) >= 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
{
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
ASKER
readFile = new System.IO.StreamReader("St atusLog.tx t");
string allRead = readFile.ReadToEnd();
readFile.Close();
//If the match is found in allRead
if (Regex.IsMatch(allRead, fileName)
{
return true;
}
else
{
return false;
}
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?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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";
ASKER
Sorry, how do you convert char to sring here?
!whitespace.Contains(input [current]) )
!whitespace.Contains(input
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()))
ASKER
never mind.
!whitespace.Contains(allRe ad[current ].ToString ()))
!whitespace.Contains(allRe
You could also change that line to:
while (current < input.Length && whitespace.IndexOf(input[current]) < 0)
ASKER
Thanks guys.
Open in new window