如何在 vb.net 中做正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13828295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do regular expressions in vb.net
提问by Darren Wainwright
So there are many questions and answers here around the subject of regular expressions. The downside is that the vast majority of answers are simply theregular expression...
所以这里有很多关于正则表达式的问题和答案。不足之处是,绝大多数的答案是简单的正则表达式...
I have also googled - and found hundreds of sites. Trying to wade through everything for a quick-to-understand and implement answer isn't too easy. they are either in a different language - which maybe shouldn't make any difference, though you escape differently in C# to VB and that leads to confusion as to what is an escape character vs a regex switch.
我也用谷歌搜索过 - 发现了数百个网站。试图通过所有内容以快速理解和实施答案并不太容易。它们要么使用不同的语言 - 这可能不会有任何区别,尽管您在 C# 中转义为 VB 的方式不同,这会导致混淆转义字符与正则表达式开关。
The part I am struggling with is understanding them so I can implement some, apparently, simple expressions.
我正在努力解决的部分是理解它们,以便我可以实现一些显然是简单的表达式。
My scenario:
我的场景:
I have to check every character in a given string, and if the regular expression doesn't allow any of the characters then it should return false.
我必须检查给定字符串中的每个字符,如果正则表达式不允许任何字符,那么它应该返回 false。
Example:
例子:
I have tried the following expressions (copy/pasted from various answers here....)
我尝试了以下表达式(从这里的各种答案中复制/粘贴....)
Dim r As New Regex("^[a-zA-Z0-9 ]*$")
also tried
也试过
Dim r As New Regex("[a-zA-Z0-9\s]")
also tried
也试过
Dim r as New Regex("^[A-Za-z0-9 ]*")
I have been implementing this like:
我一直在实施这样的:
Dim r As New Regex(_fontJIAdieuxRegEx) '' where _fontJIAdieuxRegEx is one of the above regex strings.
Dim supported = r.IsMatch(fontName)
I have been trying to validate something like the following:
我一直在尝试验证如下内容:
darrenshould return True
darren应该回来 True
da-rrenshould return Falsedue to the -hyphen
da-rrenFalse由于-连字符应该返回
da rrenshould return True
da rren应该回来 True
Now, simply put, any of these expressions will either return Truefor all of the strings or Falsefor all of the strings; so i am clearly doing something wrong.
现在,简单地说,这些表达式中的任何一个都将返回True所有字符串或False所有字符串;所以我显然做错了什么。
what I would really appreciate is someone pointing out where I am going wrong and also explain a little about the make-up of the regular expression.
我真的很感激有人指出我哪里出错了,并解释了一些关于正则表达式的组成。
Once I understand them a little more I need to be able to have different expressions to allow other characters, such as ! @ " ' . etc. So please don't just paste an expression to solve the simple example above.
一旦我对它们有更多的了解,我就需要能够有不同的表达方式来允许其他字符,例如 ! @ " ' . 等等 所以请不要只是粘贴一个表达式来解决上面的简单示例。
回答by Guffa
The first pattern is the correct one to use. The second pattern will return true if just one character in the string matches. The third pattern will return true if zero or more characters in the beginning of the string matches, which it always does.
第一种模式是正确使用的模式。如果字符串中只有一个字符匹配,则第二个模式将返回 true。如果字符串开头的零个或多个字符匹配,则第三个模式将返回 true,它总是如此。
I don't know what you did to make it not work, but using it like this works:
我不知道你做了什么让它不起作用,但像这样使用它:
Dim _fontJIAdieuxRegEx As String = "^[a-zA-Z0-9 ]*$"
Dim r As New Regex(_fontJIAdieuxRegEx)
Console.WriteLine(r.IsMatch("darren"))
Console.WriteLine(r.IsMatch("da-rren"))
Console.WriteLine(r.IsMatch("da rren"))
Output:
输出:
True
False
True
回答by Daan Callaert
The regex classes are located in the namespace System.Text.RegularExpressions. To make them available, place Imports System.Text.RegularExpressions at the start of your source code.
正则表达式类位于命名空间 System.Text.RegularExpressions 中。要使它们可用,请将 Imports System.Text.RegularExpressions 放在源代码的开头。
Regex.IsMatch("subject", "regex") checks if the regular expression matches the subject string.
Regex.IsMatch("subject", "regex") 检查正则表达式是否与主题字符串匹配。
Regex.Replace("subject", "regex", "replacement") performs a search-and-replace.
Regex.Replace("subject", "regex", "replacement") 执行搜索和替换。
Regex.Split("subject", "regex") splits the subject string into an array of strings as described above. All these methods accept an optional additional parameter of type RegexOptions, like the constructor.
Regex.Split("subject", "regex") 如上所述将主题字符串拆分为字符串数组。所有这些方法都接受一个 RegexOptions 类型的可选附加参数,如构造函数。
Source / more information: http://www.regular-expressions.info/dotnet.html
回答by Joel Coehoorn
Your 2nd expression matches anything. The *character at the end of the character class tell the regular expression engine to match that character class zeroor more times. Since there are not other conditions in the expression, any string is valid. The third expression matches anything that has at least one valid character.
您的第二个表达式匹配任何内容。将*在字符类的结束符告诉正则表达式引擎匹配字符类零次或更多次。由于表达式中没有其他条件,因此任何字符串都是有效的。第三个表达式匹配至少具有一个有效字符的任何内容。
The first expression should work, but I'm not a fan of the start and end anchors (^and $) if you don't need them. What I would do here instead is invertthe expression... look for characters that are not valid. The expression would look like this:
第一个表达式应该可以工作,但如果您不需要它们,我不喜欢开始和结束锚点(^和$)。我在这里要做的是反转表达式...查找无效的字符。表达式如下所示:
[^A-Za-z0-9 ]
[^A-Za-z0-9]
In this case, the ^character used as part of the character class means to negatethe class: this will match any character that is notin that class, and since we don't have any anchors it will match if such a character occurs anywhere in the string. Now, of course, I must also invert the result in the VB.Net code:
在这种情况下,^用作字符类的一部分的字符意味着否定该类:这将匹配不在该类中的任何字符,并且由于我们没有任何锚点,如果此类字符出现在任何位置,它将匹配字符串。现在,当然,我还必须反转 VB.Net 代码中的结果:
Dim r As New Regex("[^A-Za-z0-9 ]")
Dim supported = Not r.IsMatch(fontName)

