帮助正则表达式并试图模仿VB“喜欢”对照 [英] Help with Regex and trying to mimic the VB "like" comparison

查看:90
本文介绍了帮助正则表达式并试图模仿VB“喜欢”对照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数,为我的用户提供一个文件

过滤器。过滤器过去只使用VBLike工作。比较,

但我在C#中找不到等价物。我查看了RegEx.IsMatch,但是

它的表现完全不同。有没有办法可以模仿DOS文件名的过滤(例如*。*或*返回所有文件,*。xls

返回所有excel文件,workbook *返回所有以

" workbook"等开头的文件??


提前感谢...


andrew

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

推荐答案

您可以使用正则表达式,只允许用户键入的模式为正则表达式的模式,但有一些预防措施:

1)他们键入的任何字符都是正则表达式转义,你需要用\作为前缀,让正则表达式知道它们是文字,但要注意你可能不想在文件名中允许这些。

参见
http://msdn.microsoft.com/library/de...geElements.asp (注意wordwrap)

2)


" Andrew Baker"写道:
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:
我正在尝试编写一个函数,为我的用户提供一个文件
过滤器。过滤器过去只使用VBLike工作。比较,
但我在C#中找不到等价物。我查看了RegEx.IsMatch,但它的表现完全不同。有没有办法可以模仿DOS文件名的过滤(例如*。*或*返回所有文件,*。xls
返回所有excel文件,workbook *返回所有以
workbook等开头的文件)?

提前感谢...

andrew
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



但我不想教数百名用户如何使用常规的

表达式,因为这很复杂他们的需求。我只想要一个

根据原帖使用通配符的简单方法。


欢呼

andrew


" Beeeeeeeeeeeeves" <成为************** @ discussions.microsoft.com>在消息新闻中写道:< DB ********************************** @ microso ft.com> .. 。
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>...
你可以使用正则表达式,只允许用户输入的模式作为正则表达式的模式,但需要采取一些预防措施:
1)他们输入的任何字符都是正则表达式逃脱,你''我需要在它们前面添加\以让正则表达式知道它们是文字,但要注意你可能不想在文件名中允许这些。
请参阅
http://msdn.microsoft。 com / library / de ... geElements.asp (关注wordwrap)
2)

Andrew Baker写道:
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:
我正在尝试编写一个函数,为我的用户提供一个文件
过滤器。过滤器过去只使用VBLike工作。比较,
但我在C#中找不到等价物。我查看了RegEx.IsMatch,但它的表现完全不同。有没有办法可以模仿DOS文件名的过滤(例如*。*或*返回所有文件,*。xls
返回所有excel文件,workbook *返回所有以
workbook等开头的文件)?

提前感谢...

andrew
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



FYI设法将下面的东西放在一起,这似乎有用:

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

andrew


" Beeeeeeeeeeeeves" <成为************** @ discussions.microsoft.com>在消息新闻中写道:< DB ********************************** @ microso ft.com> .. 。
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>...
你可以使用正则表达式,只允许用户输入的模式作为正则表达式的模式,但需要采取一些预防措施:
1)他们输入的任何字符都是正则表达式逃脱,你''我需要在它们前面添加\以让正则表达式知道它们是文字,但要注意你可能不想在文件名中允许这些。
请参阅
http://msdn.microsoft。 com / library / de ... geElements.asp (关注wordwrap)
2)

Andrew Baker写道:
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:
我正在尝试编写一个函数,为我的用户提供一个文件
过滤器。过滤器过去只使用VBLike工作。比较,
但我在C#中找不到等价物。我查看了RegEx.IsMatch,但它的表现完全不同。有没有办法可以模仿DOS文件名的过滤(例如*。*或*返回所有文件,*。xls
返回所有excel文件,workbook *返回所有以
workbook等开头的文件)?

提前感谢...

andrew
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



这篇关于帮助正则表达式并试图模仿VB“喜欢”对照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆