473,396 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2120
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being...
2
by: Dave Smithz | last post by:
Hello there, Summary: How far can you go with SQL Select queries using like clauses with wildcard characters. Can you apply anything like regular expressions? Full details: On a Intranet...
1
by: Dean Slindee | last post by:
Can anyone point me to a code example or repository that would allow me to provide some "sounds like" comparison capability when doing a search for LastName? Thanks, Dean Slindee
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.