Tom Shelton's FileSystemExtension case-insensitive

M

mp

A

Arne Vajhøj

searching msdn i can't find an equivalent in c# for vb6 Like in the context
of string comparisons
there were hits in the context of sql but not string
i don't see a way to match a string with wildcard, in a case-insensitive way

You should be able to rewrite a wildcard expression to a
regex expression pretty easily. Regex supports case-insensitive.

Arne
 
M

mp

Arne Vajhøj said:
You should be able to rewrite a wildcard expression to a
regex expression pretty easily. Regex supports case-insensitive.

Arne

if that's the only way....then i'm off to find regex explanation again.
have tried to learn it in the past and it's way beyond me, but i'll spend
some time again to try and figure it out.
thanks
i sure miss 'Option Compare Text' and 'Like'
:)
mark
 
M

mp

Peter Duniho said:
I didn't look at Tom's code, but if it's not case-insensitive already on
case-insensitive file systems, it's not correct.

As Arne says, if you need to do pattern matching in a case-insensitive way
(e.g. using wildcards in file name descriptions), the Regex class might be
useful to you.

Pete

i was hoping it wouldn't come to that...:)
guess that will be my next research project
mark
 
M

mp

Peter Duniho said:
Note also that depending on how the filename comparisons are being done
now, it's possible that you can just use the String.Equals() method
overload that takes a StringComparison value as an argument.

For example, if the code splits a search pattern into wildcard and
extension, comparing just the extension to a candidate filename and
ignoring the wildcard portion, then you don't need regex to do the
extension comparison.

That said, regex is a useful tool to know. There might be some tutorials
out there, but for me the basic MSDN docs seem pretty good. I've had to do
web searches to suss out some of the more esoteric stuff, but 99% of what
I've needed to know about regex, I was able to glean from MSDN. This here
is my favorite MSDN page on the topic:
http://msdn.microsoft.com/en-us/library/az24scfc.aspx

The bulk of the information is right there, but where I need elaboration,
there are links to more in-depth discussion on regex features.

Pete

thanks, I'll check that site
i have tried several times in the past to grok the obscure (to me) regex
code symbols
i always feel kinda dumb, but when i read those tutorials on regex i feel
really really dumb!
:)
the few braincells i didn't kill off in my mis-spent youth, middle age, and
older adulthood, are now teetering on the brink of senility!
:) god it's great getting old (ha ha ha)
mark
 
M

mp

Jeff Johnson said:
that looks like a good one! thanks

so it looks like to match:
test.txt
test-this.txt
test-that.txt
test-something.txt

a regex would look like test.\.txt ?
where test is a literal, the first dot matches anything else, the second dot
being escaped matches a literal dot and the txt literal finishes it out???

mark
 
J

Jeff Johnson

so it looks like to match:
test.txt
test-this.txt
test-that.txt
test-something.txt

a regex would look like test.\.txt ?
where test is a literal, the first dot matches anything else, the second
dot being escaped matches a literal dot and the txt literal finishes it
out???

For the examples above, yes, that would be an acceptable pattern.
 
M

mp

Jeff Johnson said:
For the examples above, yes, that would be an acceptable pattern.
i don't know how to make it work in c#
I tried:
List<string> PatternList = new List<string>();
PatternList.Add("adcfAtt");
PatternList.Add("adcfAdd");
//etc

var fileQuery = from fileInfo in root.EnumerateFiles ("*" )<---tom
shelton's implementation for FindFirstFile/FindNextFile
select fileInfo;

foreach (var fileInfo in fileQuery)
{
foreach (string thisPrefix in PatternList)
{
Regex regex = new Regex(thisPrefix +
".\.lsp",RegexOptions.IgnoreCase );
if(regex.IsMatch (fileInfo.Name))
{

i get
Error 1 Unrecognized escape sequence
at the \.

so in vb6 it would be
If fileInfo.Name Like "adcfAtt*.lsp"(with option compare text)

but if i escape the backslash (the regex escape) it compiles and should
create .\. (single backslash) in the final string
Regex regex = new Regex(thisPrefix + ".\\.lsp",RegexOptions.IgnoreCase );

but it doesn't find the matches

my debug printout shows for example
adcfAdd is No match: adcfAdd-Attrib.lsp
where adcfAdd was thisPrefix and adcfAdd-Attrib.lsp was fileInfo.Name

so i'm still missing something here
thanks
mark
 
A

Arne Vajhøj

if that's the only way....then i'm off to find regex explanation again.
have tried to learn it in the past and it's way beyond me, but i'll spend
some time again to try and figure it out.

Try something like:

using System;
using System.Text.RegularExpressions;

namespace E
{
public class Wildcard
{
private Regex re;
public Wildcard(string expr)
{
re = new Regex("^" + expr.Replace(@"\", @"\\").Replace(".",
@"\.").Replace("*", @".*").Replace("?", @".") + "$",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
public bool IsMatch(string s)
{
return re.IsMatch(s);
}
}
public class Program
{
public static void Main(string[] args)
{
Wildcard wc1 = new Wildcard("*.txt");
Console.WriteLine(wc1.IsMatch("foobar.txt"));
Console.WriteLine(wc1.IsMatch("foobar.dat"));
Console.WriteLine(wc1.IsMatch("foobar.txt_old"));
Wildcard wc2 = new Wildcard("foo*.txt");
Console.WriteLine(wc2.IsMatch("foobar.txt"));
Console.WriteLine(wc2.IsMatch("foobar.dat"));
Console.WriteLine(wc2.IsMatch("foobar.txt_old"));
Wildcard wc3 = new Wildcard(@"C:\foo*.txt");
Console.WriteLine(wc3.IsMatch(@"C:\foobar.txt"));
Console.WriteLine(wc3.IsMatch(@"C:\foobar.dat"));
Console.WriteLine(wc3.IsMatch(@"C:\foobar.txt_old"));
Wildcard wc4 = new Wildcard(@"C:\foo?a?.txt");
Console.WriteLine(wc4.IsMatch(@"C:\foobar.txt"));
Console.WriteLine(wc4.IsMatch(@"C:\foobar.dat"));
Console.WriteLine(wc4.IsMatch(@"C:\foobarr.txt"));
Console.ReadKey();
}
}
}

Arne
 
M

mp

Arne Vajhøj said:
if that's the only way....then i'm off to find regex explanation again.
have tried to learn it in the past and it's way beyond me, but i'll spend
some time again to try and figure it out.

Try something like:

using System;
using System.Text.RegularExpressions;

namespace E
{
public class Wildcard
{
private Regex re;
public Wildcard(string expr)
{
re = new Regex("^" + expr.Replace(@"\", @"\\").Replace(".",
@"\.").Replace("*", @".*").Replace("?", @".") + "$", RegexOptions.Compiled
| RegexOptions.IgnoreCase);
}
public bool IsMatch(string s)
{
return re.IsMatch(s);
}
}
public class Program
{
public static void Main(string[] args)
{
Wildcard wc1 = new Wildcard("*.txt");
Console.WriteLine(wc1.IsMatch("foobar.txt"));
Console.WriteLine(wc1.IsMatch("foobar.dat"));
Console.WriteLine(wc1.IsMatch("foobar.txt_old"));
Wildcard wc2 = new Wildcard("foo*.txt");
Console.WriteLine(wc2.IsMatch("foobar.txt"));
Console.WriteLine(wc2.IsMatch("foobar.dat"));
Console.WriteLine(wc2.IsMatch("foobar.txt_old"));
Wildcard wc3 = new Wildcard(@"C:\foo*.txt");
Console.WriteLine(wc3.IsMatch(@"C:\foobar.txt"));
Console.WriteLine(wc3.IsMatch(@"C:\foobar.dat"));
Console.WriteLine(wc3.IsMatch(@"C:\foobar.txt_old"));
Wildcard wc4 = new Wildcard(@"C:\foo?a?.txt");
Console.WriteLine(wc4.IsMatch(@"C:\foobar.txt"));
Console.WriteLine(wc4.IsMatch(@"C:\foobar.dat"));
Console.WriteLine(wc4.IsMatch(@"C:\foobarr.txt"));
Console.ReadKey();
}
}
}

Arne

thanks Arne, I'll check that out
from looking at your code it appears i misunderstood something from the page
Jeff mentioned
in your sequence [ Replace("?", @".") ]
it appears . matches any ONE character (that's the ? wildcard right?)
from the tutorial i thought it was saying that . matches ANYTHING - ie
regardless of number of characters
in other words i thought . = *
mark
 
M

mp

Peter Duniho said:
I don't know what it was on the page Jeff suggested that led you to
believe "." was equivalent to "*" in a file search pattern, but it's not.

it wasn't something on the page, it was my misreading of "matches any
character"
I was missing the "matches any Single character" interpretation
thanks for the clarification on quantifiers

You may want to get familiar with the page I recommended, as it
concisely describes _exactly_ the purpose of each element in the regex
language.

Pete

thanks again, i'm studying that one also
mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top