VB6和C#正则表达式 [英] VB6 and C# regexes
问题描述
我需要将一个VB6(我不熟悉)项目转换为C#4.0。该项目包含一些字符串验证的正则表达式。
I need to convert a VB6(which I'm not fammiliar with) project to C# 4.0 one. The project contains some regexes for string validation.
我需要知道正则表达式在这两种情况下的行为是否相同,所以如果我只是从VB6项目复制regex字符串,到C#项目,他们会工作相同吗?
I need to know if the regexes behave the same in both cases, so if i just copy the regex string from the VB6 project, to the C# project, will they work the same?
我有一个正则表达式的基本知识,我可以只读一个什么,但对于风味等,
I have a basic knowledge of regexes and I can just about read what one does, but for flavors and such, that's a bit over my head at the moment.
例如,这两行是等价的吗?
For example, are these 2 lines equivalent?
VB6 :
isStringValid = (str Like "*[!0-9A-Z]*")
C#:
isStringValid = Regex.IsMatch(str, "*[!0-9A-Z]*");
$ b
谢谢!
Thanks!
推荐答案
旧的VB Like
运算符,尽管出现,不是正则表达式接口。它更多是一个glob模式匹配器。请参见 http://msdn.microsoft.com/en-us/library/swf8kaxw.aspx
The old VB Like
operator, despite appearances, is not a regular expression interface. It's more of a glob pattern matcher. See http://msdn.microsoft.com/en-us/library/swf8kaxw.aspx
在您的示例中:
Like "*[!0-9A-Z]*"
匹配以任意字符(零个或多个)那么不会匹配中间某处的字母数字字符。此正则表达式为:
Matches strings that start and end with any character (zero or more), then doesn't match an alphanumeric character somewhere in the middle. The regular expression for this would be:
/.*[^0-9A-Z].*/
EDIT ,两者不能互换使用。但是,将 Like
的操作数转换为正确的正则表达式相当容易:
EDIT To answer your question: No, the two can't be used interchangeably. However, it's fairly easy to convert Like
's operand into a proper regular expression:
Like RegEx
========== ==========
? .
* .*
# \d
[abc0-9] [abc0-9]
[!abc0-9] [^abc0-9]
这里有一些注意事项,但这应该让你开始,并且覆盖大多数情况。
There are a few caveats to this, but that should get you started and cover most cases.
这篇关于VB6和C#正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!