469,592 Members | 1,856 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

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

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
Nov 16 '05 #1
16 1903
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/de...geElements.asp (watch for wordwrap)
2)

"Andrew Baker" wrote:
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

Nov 16 '05 #2
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" <Be**************@discussions.microsoft.com> wrote in message news:<DB**********************************@microso ft.com>...
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/de...geElements.asp (watch for wordwrap)
2)

"Andrew Baker" wrote:
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

Nov 16 '05 #3
FYI have managed to bodge something together below which seems to work:

http://www.vbusers.com/codecsharp/co...1&NumReplies=0

andrew

"Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com> wrote in message news:<DB**********************************@microso ft.com>...
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/de...geElements.asp (watch for wordwrap)
2)

"Andrew Baker" wrote:
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

Nov 16 '05 #4
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" <we*******@vbusers.com> wrote in
news:c1**************************@posting.google.c om...
FYI have managed to bodge something together below which seems to work:

http://www.vbusers.com/codecsharp/co...1&NumReplies=0
andrew

"Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com> wrote in

message news:<DB**********************************@microso ft.com>...
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/de...geElements.asp
(watch for wordwrap) 2)

"Andrew Baker" wrote:
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

Nov 16 '05 #5
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]
- mv*@spam.guard.caspershouse.com

"Niki Estner" <ni*********@cube.net> wrote in message
news:eQ**************@TK2MSFTNGP11.phx.gbl...
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" <we*******@vbusers.com> wrote in
news:c1**************************@posting.google.c om...
FYI have managed to bodge something together below which seems to work:

http://www.vbusers.com/codecsharp/co...1&NumReplies=0

andrew

"Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com> wrote in

message news:<DB**********************************@microso ft.com>...
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/de...geElements.asp (watch for wordwrap) 2)

"Andrew Baker" wrote:

> 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
>


Nov 16 '05 #6
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]" <mv*@spam.guard.caspershouse.com> wrote in message news:<#V**************@TK2MSFTNGP12.phx.gbl>...
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]
- mv*@spam.guard.caspershouse.com

"Niki Estner" <ni*********@cube.net> wrote in message
news:eQ**************@TK2MSFTNGP11.phx.gbl...
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" <we*******@vbusers.com> wrote in
news:c1**************************@posting.google.c om...
FYI have managed to bodge something together below which seems to work:

http://www.vbusers.com/codecsharp/co...1&NumReplies=0

andrew

"Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com> wrote in message news:<DB**********************************@microso ft.com>... > 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/de...geElements.asp
(watch for wordwrap)
> 2)
>
> "Andrew Baker" wrote:
>
> > 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
> >


Nov 16 '05 #7
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]
- mv*@spam.guard.caspershouse.com

"Andrew Baker" <we*******@vbusers.com> wrote in message
news:c1************************@posting.google.com ...
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]" <mv*@spam.guard.caspershouse.com> wrote

in message news:<#V**************@TK2MSFTNGP12.phx.gbl>...
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]
- mv*@spam.guard.caspershouse.com

"Niki Estner" <ni*********@cube.net> wrote in message
news:eQ**************@TK2MSFTNGP11.phx.gbl...
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" <we*******@vbusers.com> wrote in
news:c1**************************@posting.google.c om...
> FYI have managed to bodge something together below which seems to work: >
>

http://www.vbusers.com/codecsharp/co...1&NumReplies=0
>
> andrew
>
> "Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com> wrote in message news:<DB**********************************@microso ft.com>...
> > 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/de...geElements.asp (watch for wordwrap)
> > 2)
> >
> > "Andrew Baker" wrote:
> >
> > > 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
> > >

Nov 16 '05 #8
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]" <mv*@spam.guard.caspershouse.com> wrote in message news:<Om**************@TK2MSFTNGP10.phx.gbl>...
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]
- mv*@spam.guard.caspershouse.com

"Andrew Baker" <we*******@vbusers.com> wrote in message
news:c1************************@posting.google.com ...
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]" <mv*@spam.guard.caspershouse.com> wrote

in message news:<#V**************@TK2MSFTNGP12.phx.gbl>...
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]
- mv*@spam.guard.caspershouse.com

"Niki Estner" <ni*********@cube.net> wrote in message
news:eQ**************@TK2MSFTNGP11.phx.gbl...
> 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" <we*******@vbusers.com> wrote in
> news:c1**************************@posting.google.c om...
> > FYI have managed to bodge something together below which seems to work: > >
> >
>
http://www.vbusers.com/codecsharp/co...1&NumReplies=0 > >
> > andrew
> >
> > "Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com> wrote in
message news:<DB**********************************@microso ft.com>... > > > 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/de...geElements.asp > (watch for wordwrap)
> > > 2)
> > >
> > > "Andrew Baker" wrote:
> > >
> > > > 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
> > > >
>
>

Nov 16 '05 #9
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]" <mv*@spam.guard.caspershouse.com> wrote in message news:<Om**************@TK2MSFTNGP10.phx.gbl>...
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]
- mv*@spam.guard.caspershouse.com

"Andrew Baker" <we*******@vbusers.com> wrote in message
news:c1************************@posting.google.com ...
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]" <mv*@spam.guard.caspershouse.com> wrote

in message news:<#V**************@TK2MSFTNGP12.phx.gbl>...
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]
- mv*@spam.guard.caspershouse.com

"Niki Estner" <ni*********@cube.net> wrote in message
news:eQ**************@TK2MSFTNGP11.phx.gbl...
> 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" <we*******@vbusers.com> wrote in
> news:c1**************************@posting.google.c om...
> > FYI have managed to bodge something together below which seems to work: > >
> >
>
http://www.vbusers.com/codecsharp/co...1&NumReplies=0 > >
> > andrew
> >
> > "Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com> wrote in
message news:<DB**********************************@microso ft.com>... > > > 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/de...geElements.asp > (watch for wordwrap)
> > > 2)
> > >
> > > "Andrew Baker" wrote:
> > >
> > > > 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
> > > >
>
>

Nov 16 '05 #10
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" <we*******@vbusers.com> wrote in
news:c1**************************@posting.google.c om...
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]" <mv*@spam.guard.caspershouse.com> wrote

in message news:<Om**************@TK2MSFTNGP10.phx.gbl>...
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]
- mv*@spam.guard.caspershouse.com

"Andrew Baker" <we*******@vbusers.com> wrote in message
news:c1************************@posting.google.com ...
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]" <mv*@spam.guard.caspershouse.com> wrote
in message news:<#V**************@TK2MSFTNGP12.phx.gbl>...
> 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]
> - mv*@spam.guard.caspershouse.com
>
> "Niki Estner" <ni*********@cube.net> wrote in message
> news:eQ**************@TK2MSFTNGP11.phx.gbl...
> > 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" <we*******@vbusers.com> wrote in
> > news:c1**************************@posting.google.c om...
> > > FYI have managed to bodge something together below which seems
to work:
> > >
> > >
> >
>

http://www.vbusers.com/codecsharp/co...1&NumReplies=0 > > >
> > > andrew
> > >
> > > "Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com>

wrote in
message news:<DB**********************************@microso ft.com>...
> > > > 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/de...geElements.asp > > (watch for wordwrap)
> > > > 2)
> > > >
> > > > "Andrew Baker" wrote:
> > > >
> > > > > 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
> > > > >
> >
> >

Nov 16 '05 #11
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" <we*******@vbusers.com> wrote in
news:c1**************************@posting.google.c om...
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]" <mv*@spam.guard.caspershouse.com> wrote

in message news:<Om**************@TK2MSFTNGP10.phx.gbl>...
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]
- mv*@spam.guard.caspershouse.com

"Andrew Baker" <we*******@vbusers.com> wrote in message
news:c1************************@posting.google.com ...
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]" <mv*@spam.guard.caspershouse.com> wrote
in message news:<#V**************@TK2MSFTNGP12.phx.gbl>...
> 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]
> - mv*@spam.guard.caspershouse.com
>
> "Niki Estner" <ni*********@cube.net> wrote in message
> news:eQ**************@TK2MSFTNGP11.phx.gbl...
> > 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" <we*******@vbusers.com> wrote in
> > news:c1**************************@posting.google.c om...
> > > FYI have managed to bodge something together below which seems
to work:
> > >
> > >
> >
>

http://www.vbusers.com/codecsharp/co...1&NumReplies=0 > > >
> > > andrew
> > >
> > > "Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com>

wrote in
message news:<DB**********************************@microso ft.com>...
> > > > 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/de...geElements.asp > > (watch for wordwrap)
> > > > 2)
> > > >
> > > > "Andrew Baker" wrote:
> > > >
> > > > > 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
> > > > >
> >
> >

Nov 16 '05 #12
My immediate thought was:

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

:)

andrew
"Niki Estner" <ni*********@cube.net> wrote in message news:<up*************@TK2MSFTNGP12.phx.gbl>...
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" <we*******@vbusers.com> wrote in
news:c1**************************@posting.google.c om...
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]" <mv*@spam.guard.caspershouse.com> wrote

in message news:<Om**************@TK2MSFTNGP10.phx.gbl>...
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]
- mv*@spam.guard.caspershouse.com

"Andrew Baker" <we*******@vbusers.com> wrote in message
news:c1************************@posting.google.com ...
> 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]" <mv*@spam.guard.caspershouse.com> wrote
in message news:<#V**************@TK2MSFTNGP12.phx.gbl>... > > 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]
> > - mv*@spam.guard.caspershouse.com
> >
> > "Niki Estner" <ni*********@cube.net> wrote in message
> > news:eQ**************@TK2MSFTNGP11.phx.gbl...
> > > 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" <we*******@vbusers.com> wrote in
> > > news:c1**************************@posting.google.c om...
> > > > FYI have managed to bodge something together below which seems to
work: > > > >
> > > >
> > >
> >
http://www.vbusers.com/codecsharp/co...1&NumReplies=0 > > > >
> > > > andrew
> > > >
> > > > "Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com>
wrote in
message news:<DB**********************************@microso ft.com>...
> > > > > 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/de...geElements.asp > > > (watch for wordwrap)
> > > > > 2)
> > > > >
> > > > > "Andrew Baker" wrote:
> > > > >
> > > > > > 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
> > > > > >
> > >
> > >

Nov 16 '05 #13
My immediate thought was:

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

:)

andrew
"Niki Estner" <ni*********@cube.net> wrote in message news:<up*************@TK2MSFTNGP12.phx.gbl>...
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" <we*******@vbusers.com> wrote in
news:c1**************************@posting.google.c om...
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]" <mv*@spam.guard.caspershouse.com> wrote

in message news:<Om**************@TK2MSFTNGP10.phx.gbl>...
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]
- mv*@spam.guard.caspershouse.com

"Andrew Baker" <we*******@vbusers.com> wrote in message
news:c1************************@posting.google.com ...
> 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]" <mv*@spam.guard.caspershouse.com> wrote
in message news:<#V**************@TK2MSFTNGP12.phx.gbl>... > > 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]
> > - mv*@spam.guard.caspershouse.com
> >
> > "Niki Estner" <ni*********@cube.net> wrote in message
> > news:eQ**************@TK2MSFTNGP11.phx.gbl...
> > > 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" <we*******@vbusers.com> wrote in
> > > news:c1**************************@posting.google.c om...
> > > > FYI have managed to bodge something together below which seems to
work: > > > >
> > > >
> > >
> >
http://www.vbusers.com/codecsharp/co...1&NumReplies=0 > > > >
> > > > andrew
> > > >
> > > > "Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com>
wrote in
message news:<DB**********************************@microso ft.com>...
> > > > > 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/de...geElements.asp > > > (watch for wordwrap)
> > > > > 2)
> > > > >
> > > > > "Andrew Baker" wrote:
> > > > >
> > > > > > 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
> > > > > >
> > >
> > >

Nov 16 '05 #14
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" wrote:
FYI have managed to bodge something together below which seems to work:

http://www.vbusers.com/codecsharp/co...1&NumReplies=0

andrew

"Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com> wrote in message news:<DB**********************************@microso ft.com>...
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/de...geElements.asp (watch for wordwrap)
2)

"Andrew Baker" wrote:
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

Nov 16 '05 #15
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:<74**********************************@microso ft.com>...
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" wrote:
FYI have managed to bodge something together below which seems to work:

http://www.vbusers.com/codecsharp/co...1&NumReplies=0

andrew

"Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com> wrote in message news:<DB**********************************@microso ft.com>...
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/de...geElements.asp (watch for wordwrap)
2)

"Andrew Baker" wrote:

> 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
>

Nov 16 '05 #16
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:<85**********************************@microso ft.com>...
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/de...fwildcards.asp

Thank you Andrew! Take care.

"Andrew Baker" wrote:
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:<74**********************************@microso ft.com>...
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" wrote:

> FYI have managed to bodge something together below which seems to work:
>
> http://www.vbusers.com/codecsharp/co...1&NumReplies=0
>
> andrew
>
> "Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com> wrote in message news:<DB**********************************@microso ft.com>...
> > 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/de...geElements.asp (watch for wordwrap)
> > 2)
> >
> > "Andrew Baker" wrote:
> >
> > > 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
> > >
>

Nov 16 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.