C# 如何在我的电子邮件验证中读取正则表达式
我找到了一个使用regex验证电子邮件的解决方案。 但我真的不明白如何正确阅读它,因为这是我第一次使用regex。 谁能给我解释一下怎样用文字读它吗?因为用if语句创建这个将是一件痛苦的事 我的代码:C# 如何在我的电子邮件验证中读取正则表达式,c#,regex,winforms,C#,Regex,Winforms,我找到了一个使用regex验证电子邮件的解决方案。 但我真的不明白如何正确阅读它,因为这是我第一次使用regex。 谁能给我解释一下怎样用文字读它吗?因为用if语句创建这个将是一件痛苦的事 我的代码: if (string.IsNullOrWhiteSpace(textBox4.Text)) { label8.Text = "E-pasts netika ievadīts!"; } else { Regex emailExpression = new Regex(@"^[a-zA
if (string.IsNullOrWhiteSpace(textBox4.Text))
{
label8.Text = "E-pasts netika ievadīts!";
}
else
{
Regex emailExpression = new Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
if (emailExpression.IsMatch(textBox4.Text))
{
label8.Text = "";
}
else
{
label8.Text = "E-pasts ievadīts nepareizi!";
}
}
对于regex,请点击以下链接: 这里粘贴您的正则表达式
[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z].
将鼠标悬停在每个文本上,您将了解它们的含义
举几个例子:
a-z
将匹配a到z范围内的字符
A-Z
将匹配A-Z范围内的字符
\w
匹配任何单词字符
{2,28}
前面标记的2和28之间的匹配对于正则表达式,请遵循以下链接:
这里粘贴您的正则表达式[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z].
将鼠标悬停在每个文本上,您将了解它们的含义
举几个例子:
a-z
将匹配a到z范围内的字符
A-Z
将匹配A-Z范围内的字符
\w
匹配任何单词字符
{2,28}
前面标记的2和28之间的匹配以下是细分。请注意,所有[]
都是单个项目的集合。以下可以是*
(零或多个)或+
一个或多个或{,}
(最小和最大)事件
如果一个人评论模式以显示一个固有的模式,那么一旦在关键点上做了标记,实际上更容易理解;我将这样做:
var pattern = @"
^ # Beginning of line, data must start here instead of matching any location *away from the start*.
[a-zA-Z] # A set of characters from a-z and A-Z but just one
[\w\.-]{2,28} # A set of any word character \w=a-zA-Z0-9 with a literal period and dash; but minimum of 2 to a max of 28 characters.
[a-zA-Z0-9] # Could be replaced with a single \w; this ensure that there is a character and not a non character before the @
@ # Literal `@`
[a-zA-Z0-9] # same as \w; ensures a character
[\w\.-]* # '*' = Zero or more of the set with a literal period in the set.
[a-zA-Z0-9] # same as \w; ensures a character
\. # Literal Period must be matched.
[a-zA-Z] # A set of characters from a-z and A-Z but just one
[a-zA-Z\.]* # zero or more characters with a literal period `.`.
[a-zA-Z] # same as \w; ensures a character
$ # End; contrains the whole match with the `^` so no loose characters are left out of the match
";
// IgnorePatternWhitespace allows us to comment the pattern; does not affect processing in any way.
Regex.IsMatch("abcd@def.com", pattern, RegexOptions.IgnorePatternWhitespace); // True/Valid
Regex.IsMatch("1abcd@def.com", pattern, RegexOptions.IgnorePatternWhitespace);// False/Invalid
Regex.IsMatch("abc@def.com", pattern, RegexOptions.IgnorePatternWhitespace); // False/Invalid
注意,测试#3中发现的模式中存在逻辑缺陷。对于
abc@def.com
将失败,因为模式在@
之前的第一个字符和最后一个字符前缀之间至少查找2个字符。我会把它改成一个*
,意思是零或更多。这里是分类。请注意,所有[]
都是单个项目的集合。以下可以是*
(零或多个)或+
一个或多个或{,}
(最小和最大)事件
如果一个人评论模式以显示一个固有的模式,那么一旦在关键点上做了标记,实际上更容易理解;我将这样做:
var pattern = @"
^ # Beginning of line, data must start here instead of matching any location *away from the start*.
[a-zA-Z] # A set of characters from a-z and A-Z but just one
[\w\.-]{2,28} # A set of any word character \w=a-zA-Z0-9 with a literal period and dash; but minimum of 2 to a max of 28 characters.
[a-zA-Z0-9] # Could be replaced with a single \w; this ensure that there is a character and not a non character before the @
@ # Literal `@`
[a-zA-Z0-9] # same as \w; ensures a character
[\w\.-]* # '*' = Zero or more of the set with a literal period in the set.
[a-zA-Z0-9] # same as \w; ensures a character
\. # Literal Period must be matched.
[a-zA-Z] # A set of characters from a-z and A-Z but just one
[a-zA-Z\.]* # zero or more characters with a literal period `.`.
[a-zA-Z] # same as \w; ensures a character
$ # End; contrains the whole match with the `^` so no loose characters are left out of the match
";
// IgnorePatternWhitespace allows us to comment the pattern; does not affect processing in any way.
Regex.IsMatch("abcd@def.com", pattern, RegexOptions.IgnorePatternWhitespace); // True/Valid
Regex.IsMatch("1abcd@def.com", pattern, RegexOptions.IgnorePatternWhitespace);// False/Invalid
Regex.IsMatch("abc@def.com", pattern, RegexOptions.IgnorePatternWhitespace); // False/Invalid
注意,测试#3中发现的模式中存在逻辑缺陷。对于
abc@def.com
将失败,因为模式在@
之前的第一个字符和最后一个字符前缀之间至少查找2个字符。我会改为*
,意思是零或更多。我可以推荐LeaVerou关于正则表达式的演讲。看,我可以推荐Lea Verou关于regex的演讲。看见