Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

36 views
Skip to first unread message

Andrew Baker

unread,
Jun 15, 2004, 5:55:08 AM6/15/04
to
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

Beeeeeeeeeeeeves

unread,
Jun 15, 2004, 6:29:01 AM6/15/04
to
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/default.asp?url=/library/en-us/cpgenref/html/cpconRegularExpressionsLanguageElements.asp (watch for wordwrap)
2)

Andrew Baker

unread,
Jun 15, 2004, 10:54:24 AM6/15/04
to
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

"Beeeeeeeeeeeeves" <Beeeeeee...@discussions.microsoft.com> wrote in message news:<DB0F13A1-8FC8-4210...@microsoft.com>...

Andrew Baker

unread,
Jun 15, 2004, 11:20:39 AM6/15/04
to
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" <Beeeeeee...@discussions.microsoft.com> wrote in message news:<DB0F13A1-8FC8-4210...@microsoft.com>...

Niki Estner

unread,
Jun 15, 2004, 12:48:05 PM6/15/04
to
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" <webm...@vbusers.com> wrote in
news:c19b84e5.04061...@posting.google.com...

Nicholas Paldino [.NET/C# MVP]

unread,
Jun 15, 2004, 2:09:25 PM6/15/04
to
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]
- m...@spam.guard.caspershouse.com

"Niki Estner" <niki....@cube.net> wrote in message
news:eQWjNivU...@TK2MSFTNGP11.phx.gbl...

Andrew Baker

unread,
Jun 16, 2004, 2:33:14 AM6/16/04
to
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]" <m...@spam.guard.caspershouse.com> wrote in message news:<#VkveQwU...@TK2MSFTNGP12.phx.gbl>...

Nicholas Paldino [.NET/C# MVP]

unread,
Jun 16, 2004, 10:15:03 AM6/16/04
to
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]
- m...@spam.guard.caspershouse.com

"Andrew Baker" <webm...@vbusers.com> wrote in message
news:c19b84e5.0406...@posting.google.com...

Andrew Baker

unread,
Jun 16, 2004, 3:50:14 PM6/16/04
to
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]" <m...@spam.guard.caspershouse.com> wrote in message news:<OmQPMy6U...@TK2MSFTNGP10.phx.gbl>...

Niki Estner

unread,
Jun 16, 2004, 5:50:31 PM6/16/04
to
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

unread,
Jun 17, 2004, 4:07:33 AM6/17/04
to
My immediate thought was:

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

:)

andrew


"Niki Estner" <niki....@cube.net> wrote in message news:<upTb3v#UEHA...@TK2MSFTNGP12.phx.gbl>...

RobC

unread,
Jul 23, 2004, 7:06:02 PM7/23/04
to
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));

Andrew Baker

unread,
Jul 24, 2004, 7:47:38 AM7/24/04
to
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

"RobC" <Ro...@discussions.microsoft.com> wrote in message news:<74AB1D1F-DC66-41DF...@microsoft.com>...

RobC

unread,
Jul 27, 2004, 12:30:04 PM7/27/04
to
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
val = (Like(@"workbook.xls",@"work?????.*",true));
should return false since "work" is only followed by four characters and the search pattern is asking for 5.

Ref: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/vxgrfwildcards.asp

Thank you Andrew! Take care.

Andrew Baker

unread,
Jul 29, 2004, 9:04:35 AM7/29/04
to
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" <Ro...@discussions.microsoft.com> wrote in message news:<8567B6CC-E9EA-41DB...@microsoft.com>...

0 new messages