Help with Regex and trying to mimic the VB "like" comparison

  • Thread starter Thread starter Andrew Baker
  • Start date Start date
A

Andrew Baker

I am trying to write a function which provides my users with a file
filter. The filter used to work just using the VB "Like" comparision,
but I can't find the equivilant in C#. I looked at RegEx.IsMatch but
it behaves quite differently. Is there a way I can mimic the DOS
filtering of filenames (eg. "*.*" or "*" returns all files, "*.xls"
returns all excel files, "workbook*" returns all files begining with
"workbook" etc)?

thanks in advance...

andrew
 
But I don't want to have to teach hundreds of users how to use regular
expressions, as this is far to complex for their needs. I just want a
simple way of using a wildcard character as per my original post.

cheers
andrew
 
After a brief look, I think this code won't work for strings containing
backslashes, braces, or any other Regex-metacharacters.
I'd suggest using Regex.Escape(...) to escape all the meta-characters in the
pattern, and then replacing "\*" to ".*", and "\?" to ".", and adding the
"^"/"$" prefix/suffix.

Niki

PS: Maybe your users would be glad if they had the choice to use the whole
power of Regex's, too (optionally, of course). Like in the Search-Replace
dialog of VS.

Andrew Baker said:
FYI have managed to bodge something together below which seems to work:

http://www.vbusers.com/codecsharp/codeget.asp?ThreadID=25&PostID=1&NumReplies=0

andrew

"Beeeeeeeeeeeeves" <[email protected]> wrote in
prefix them with \ to let the regex know they are literals, but beware that
you may not want to allow these in the filename anyway.http://msdn.microsoft.com/library/d...l/cpconRegularExpressionsLanguageElements.asp
(watch for wordwrap)
 
Niki, Beeeeeeeeeeeeves, and Andrew,

The Like functionality is exposed through the Microsoft.VisualBasic
namespace. Add a reference to Microsoft.VisualBasic.dll, and then call the
static StrLike method on the StringType class in the
Microsoft.VisualBasic.CompilerServices namespace.

Hope this helps.
 
thanks guys,

some excellent ideas. Unfortunately, we are "banned" from using VB.NET
and they packaging team have actually removed it from the
installation! Thanks Nikki for the Regex.Escape tip, this worked a
charm...

cheers again
andrew

Nicholas Paldino said:
Niki, Beeeeeeeeeeeeves, and Andrew,

The Like functionality is exposed through the Microsoft.VisualBasic
namespace. Add a reference to Microsoft.VisualBasic.dll, and then call the
static StrLike method on the StringType class in the
Microsoft.VisualBasic.CompilerServices namespace.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Niki Estner said:
After a brief look, I think this code won't work for strings containing
backslashes, braces, or any other Regex-metacharacters.
I'd suggest using Regex.Escape(...) to escape all the meta-characters in the
pattern, and then replacing "\*" to ".*", and "\?" to ".", and adding the
"^"/"$" prefix/suffix.

Niki

PS: Maybe your users would be glad if they had the choice to use the whole
power of Regex's, too (optionally, of course). Like in the Search-Replace
dialog of VS.


http://www.vbusers.com/codecsharp/codeget.asp?ThreadID=25&PostID=1&NumReplies=0
prefix them with \ to let the regex know they are literals, but beware that
you may not want to allow these in the filename anyway.
http://msdn.microsoft.com/library/d...l/cpconRegularExpressionsLanguageElements.asp
(watch for wordwrap)
 
Andrew,

If you don't mind me asking, what is the purpose of that? Why are you
actually changing the install of the standard framework? This is a BAD
decision, IMO, because there might be areas of the framework that depend on
this (unlikely, but it is possible, since the assumption is that the WHOLE
framework will be installed).

This is really shady stuff, IMO.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andrew Baker said:
thanks guys,

some excellent ideas. Unfortunately, we are "banned" from using VB.NET
and they packaging team have actually removed it from the
installation! Thanks Nikki for the Regex.Escape tip, this worked a
charm...

cheers again
andrew

"Nicholas Paldino [.NET/C# MVP]" <[email protected]> wrote
in message news: said:
Niki, Beeeeeeeeeeeeves, and Andrew,

The Like functionality is exposed through the Microsoft.VisualBasic
namespace. Add a reference to Microsoft.VisualBasic.dll, and then call the
static StrLike method on the StringType class in the
Microsoft.VisualBasic.CompilerServices namespace.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Niki Estner said:
After a brief look, I think this code won't work for strings containing
backslashes, braces, or any other Regex-metacharacters.
I'd suggest using Regex.Escape(...) to escape all the meta-characters
in
the
pattern, and then replacing "\*" to ".*", and "\?" to ".", and adding the
"^"/"$" prefix/suffix.

Niki

PS: Maybe your users would be glad if they had the choice to use the whole
power of Regex's, too (optionally, of course). Like in the Search-Replace
dialog of VS.

FYI have managed to bodge something together below which seems to work:
http://www.vbusers.com/codecsharp/codeget.asp?ThreadID=25&PostID=1&NumReplies=0
andrew

"Beeeeeeeeeeeeves" <[email protected]>
wrote in
message news: said:
You can use regexes, just allow the pattern the user types be the
pattern for the regex, but with a few precautions:
1) Any characters they type that are regex escapes, you'll need to
prefix them with \ to let the regex know they are literals, but beware that
you may not want to allow these in the filename anyway.
See
http://msdn.microsoft.com/library/d...l/cpconRegularExpressionsLanguageElements.asp
(watch for wordwrap)
2)

:

I am trying to write a function which provides my users with a file
filter. The filter used to work just using the VB "Like" comparision,
but I can't find the equivilant in C#. I looked at RegEx.IsMatch but
it behaves quite differently. Is there a way I can mimic the DOS
filtering of filenames (eg. "*.*" or "*" returns all files, "*.xls"
returns all excel files, "workbook*" returns all files begining with
"workbook" etc)?

thanks in advance...

andrew
 
Nicholas,

I understand what you are saying and would normally agree, but this is
not my policy. There was a long discussion about it and eventually it
was decided the VB.NET would be removed from VS.NET install. This
doesn't affect the running of the framework and is being used by
hundereds of developers throughout my organisation. But as you can
imagine, old VB programmers occassionally have to write a few extra
routines to feel comfortable in C#!

So it's just one of those things I have to work around from time to
time...

andrew


Nicholas Paldino said:
Andrew,

If you don't mind me asking, what is the purpose of that? Why are you
actually changing the install of the standard framework? This is a BAD
decision, IMO, because there might be areas of the framework that depend on
this (unlikely, but it is possible, since the assumption is that the WHOLE
framework will be installed).

This is really shady stuff, IMO.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andrew Baker said:
thanks guys,

some excellent ideas. Unfortunately, we are "banned" from using VB.NET
and they packaging team have actually removed it from the
installation! Thanks Nikki for the Regex.Escape tip, this worked a
charm...

cheers again
andrew

"Nicholas Paldino [.NET/C# MVP]" <[email protected]> wrote
in message news: said:
Niki, Beeeeeeeeeeeeves, and Andrew,

The Like functionality is exposed through the Microsoft.VisualBasic
namespace. Add a reference to Microsoft.VisualBasic.dll, and then call the
static StrLike method on the StringType class in the
Microsoft.VisualBasic.CompilerServices namespace.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

After a brief look, I think this code won't work for strings containing
backslashes, braces, or any other Regex-metacharacters.
I'd suggest using Regex.Escape(...) to escape all the meta-characters
in
the
pattern, and then replacing "\*" to ".*", and "\?" to ".", and adding the
"^"/"$" prefix/suffix.

Niki

PS: Maybe your users would be glad if they had the choice to use the whole
power of Regex's, too (optionally, of course). Like in the Search-Replace
dialog of VS.

FYI have managed to bodge something together below which seems to work:



http://www.vbusers.com/codecsharp/codeget.asp?ThreadID=25&PostID=1&NumReplies=0

andrew

"Beeeeeeeeeeeeves" <[email protected]>
wrote in
 
I think what Nicholas is trying to say is that "Microsoft.VisualBasic.dll"
is not a part of Visual Studio or some VB development environment: It's a
part of the .net framework. It was created for 'old VB programmers', but
there's nothing "bad" about them. And removing them from a working system
might prevent "good software" from working.

BTW: The same applies to java compatiblity libraries;

Niki

Andrew Baker said:
Nicholas,

I understand what you are saying and would normally agree, but this is
not my policy. There was a long discussion about it and eventually it
was decided the VB.NET would be removed from VS.NET install. This
doesn't affect the running of the framework and is being used by
hundereds of developers throughout my organisation. But as you can
imagine, old VB programmers occassionally have to write a few extra
routines to feel comfortable in C#!

So it's just one of those things I have to work around from time to
time...

andrew


"Nicholas Paldino [.NET/C# MVP]" <[email protected]> wrote
in message news: said:
Andrew,

If you don't mind me asking, what is the purpose of that? Why are you
actually changing the install of the standard framework? This is a BAD
decision, IMO, because there might be areas of the framework that depend on
this (unlikely, but it is possible, since the assumption is that the WHOLE
framework will be installed).

This is really shady stuff, IMO.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andrew Baker said:
thanks guys,

some excellent ideas. Unfortunately, we are "banned" from using VB.NET
and they packaging team have actually removed it from the
installation! Thanks Nikki for the Regex.Escape tip, this worked a
charm...

cheers again
andrew

"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
in message news: said:
Niki, Beeeeeeeeeeeeves, and Andrew,

The Like functionality is exposed through the Microsoft.VisualBasic
namespace. Add a reference to Microsoft.VisualBasic.dll, and then
call
the
static StrLike method on the StringType class in the
Microsoft.VisualBasic.CompilerServices namespace.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

After a brief look, I think this code won't work for strings containing
backslashes, braces, or any other Regex-metacharacters.
I'd suggest using Regex.Escape(...) to escape all the
meta-characters
in
the
pattern, and then replacing "\*" to ".*", and "\?" to ".", and
adding
the
"^"/"$" prefix/suffix.

Niki

PS: Maybe your users would be glad if they had the choice to use
the
whole
power of Regex's, too (optionally, of course). Like in the Search-Replace
dialog of VS.

FYI have managed to bodge something together below which seems
to
work: http://www.vbusers.com/codecsharp/codeget.asp?ThreadID=25&PostID=1&NumReplies=0
andrew

"Beeeeeeeeeeeeves" <[email protected]>
wrote in
message news: said:
You can use regexes, just allow the pattern the user types be
the
pattern for the regex, but with a few precautions:
1) Any characters they type that are regex escapes, you'll need to
prefix them with \ to let the regex know they are literals, but
beware
that
you may not want to allow these in the filename anyway.
See
http://msdn.microsoft.com/library/d...l/cpconRegularExpressionsLanguageElements.asp
(watch for wordwrap)
2)

:

I am trying to write a function which provides my users with
a
file
filter. The filter used to work just using the VB "Like" comparision,
but I can't find the equivilant in C#. I looked at
RegEx.IsMatch
but
it behaves quite differently. Is there a way I can mimic the DOS
filtering of filenames (eg. "*.*" or "*" returns all files, "*.xls"
returns all excel files, "workbook*" returns all files
begining
with
"workbook" etc)?

thanks in advance...

andrew
 
My immediate thought was:

don't worry about that... we don't have any "good software"!!!!

:)

andrew


Niki Estner said:
I think what Nicholas is trying to say is that "Microsoft.VisualBasic.dll"
is not a part of Visual Studio or some VB development environment: It's a
part of the .net framework. It was created for 'old VB programmers', but
there's nothing "bad" about them. And removing them from a working system
might prevent "good software" from working.

BTW: The same applies to java compatiblity libraries;

Niki

Andrew Baker said:
Nicholas,

I understand what you are saying and would normally agree, but this is
not my policy. There was a long discussion about it and eventually it
was decided the VB.NET would be removed from VS.NET install. This
doesn't affect the running of the framework and is being used by
hundereds of developers throughout my organisation. But as you can
imagine, old VB programmers occassionally have to write a few extra
routines to feel comfortable in C#!

So it's just one of those things I have to work around from time to
time...

andrew


"Nicholas Paldino [.NET/C# MVP]" <[email protected]> wrote
in message news: said:
Andrew,

If you don't mind me asking, what is the purpose of that? Why are you
actually changing the install of the standard framework? This is a BAD
decision, IMO, because there might be areas of the framework that depend on
this (unlikely, but it is possible, since the assumption is that the WHOLE
framework will be installed).

This is really shady stuff, IMO.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

thanks guys,

some excellent ideas. Unfortunately, we are "banned" from using VB.NET
and they packaging team have actually removed it from the
installation! Thanks Nikki for the Regex.Escape tip, this worked a
charm...

cheers again
andrew

"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
in message news: said:
Niki, Beeeeeeeeeeeeves, and Andrew,

The Like functionality is exposed through the Microsoft.VisualBasic
namespace. Add a reference to Microsoft.VisualBasic.dll, and then
call
the
static StrLike method on the StringType class in the
Microsoft.VisualBasic.CompilerServices namespace.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

After a brief look, I think this code won't work for strings containing
backslashes, braces, or any other Regex-metacharacters.
I'd suggest using Regex.Escape(...) to escape all the meta-characters
in
the
pattern, and then replacing "\*" to ".*", and "\?" to ".", and
adding
the
"^"/"$" prefix/suffix.

Niki

PS: Maybe your users would be glad if they had the choice to use
the
whole
power of Regex's, too (optionally, of course). Like in the Search-Replace
dialog of VS.

FYI have managed to bodge something together below which seems
to
work:
http://www.vbusers.com/codecsharp/codeget.asp?ThreadID=25&PostID=1&NumReplies=0

andrew

"Beeeeeeeeeeeeves" <[email protected]>
wrote in
message You can use regexes, just allow the pattern the user types be
the
pattern for the regex, but with a few precautions:
beware
that a
file RegEx.IsMatch
but begining
with
 
Andrew, I believe the following should return false, but return true:
val = (Like(@"workbook.xls",@"work*xls",true));
val = (Like(@"abcxls",@"*xls?",true));
val = (Like(@"workbook.xls",@"work?????.*",true));
val = (Like(@"abcxls",@"????xls",true));
val = (Like(@"workbook.xls",@"work?????.*",true));
 
I think it's correct that they return true since ? matches zero or
more instances of a character (see this article on Regular Expressions
http://www.cs.tut.fi/~jkorpela/perl/regexp.html).

However I updated the code to support + which matches one or more
instances (which I think is what you are looking for)...

regards,
andrew
 
Your absolutely correct. I have made another fist of trying to get
this to work. Please let me know if this fixes the problem...

regards
andrew
www.vbusers.com

RobC said:
Thank you for answering Andrew! The question marks I am refering to
are part of the search pattern, not the regular expression. The
solution provided previously (LIKE) accepted a file system search
pattern and used a regular expression to mimick a file name search. A
file system search pattern consists of *,? and #, where ? represents
one and only one char of any kind, * represents one of more chars of
any kind, and # represents one and only one numeric. So, work?????.*
would mean to find files with any extionsion and whose file name is 10
bytes long, beginning with "work" and followed by any 5 characters. +
is not valid in a search pattern. Having said that I believe
 
Back
Top