CAtlRegExp用于匹配最多4个字符的正则表达式

时间:2022-09-28 23:18:53

Short version:

How can I get a regex that matches a@a.aaaa but not a@a.aaaaa using CAtlRegExp ?

如何获得匹配a@a的regex。aaaa级但不是a@a。五星级使用CAtlRegExp ?


Long version:

I'm using CAtlRegExp http://msdn.microsoft.com/en-us/library/k3zs4axe(VS.80).aspx to try to match email addresses. I want to use the regex

我使用CAtlRegExp http://msdn.microsoft.com/en-us/library/k3zs4axe(VS.80).aspx来匹配电子邮件地址。我想用regex

^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$

extracted from here. But the syntax that CAtlRegExp accepts is different than the one used there. This regex returns the error REPARSE_ERROR_BRACKET_EXPECTED, you can check for yourself using this app: http://www.codeproject.com/KB/string/mfcregex.aspx

从这里提取。但是CAtlRegExp接受的语法与这里使用的语法不同。这个regex返回了预期的error errse_error_bracket_expected,您可以使用这个应用程序:http://www.codeproject.com/KB/string/mfcregex.aspx自行检查

Using said app, I created this regex:

使用上述app,我创建了这个regex:

^[a-zA-Z0-9\._%\+\-]+@([a-zA-Z0-9-]+\.)+[a-zA-Z]$

But the problem is this matches a@a.aaaaa as valid, I need it to match 4 characters maximum for the op-level domain.

但问题是它与a@a匹配。aaaaa为有效,我需要它匹配操作级域的最大4个字符。

So, how can I get a regex that matches a@a.aaaa but not a@a.aaaaa ?

那么,如何获得匹配a@a的regex呢?aaaa级但不是a@a。五星级的呢?

2 个解决方案

#1


2  

Try: ^[a-zA-Z0-9\._%\+\-]+@([a-zA-Z0-9-]+\.)+\c\c\c?\c?$

试题:^[a-zA-Z0-9 \ ._ % \ + \ -]+ @([a-zA-Z0-9 -]+ \)+ \ \ c \ c ?美元\ c ?

This expression replaces the [A-Z]{2,4} sequence which CAtlRegExp doesn't support with \c\c\c?\c?

这个表达式替代了CAtlRegExp不支持的[A-Z]{2,4}序列。

\c serves as an abbreviation of [a-zA-Z]. The question marks after the 3rd and 4th \c's indicate they can match either zero or one characters. As a result, this portion of the expression matches 2, 3 or 4 characters, but neither more nor less.

\c是[a-zA-Z]的缩写。第三和第四\c后面的问号表示它们可以匹配零或一个字符。因此,表达式的这一部分匹配2、3或4个字符,但不会多或少。

#2


1  

You are trying to match email addresses, a very widely used critical element of internet communication.

您正在尝试匹配电子邮件地址,一个非常广泛使用的关键因素,互联网通信。

To which I would say that this job is best done with the most widely used most correct regex.

对此,我认为最好使用最广泛使用的最正确的regex来完成这项工作。

Since email address format rules are described by RFC822, it seems useful to do internet searches for something like "RFC822 email regex".

由于RFC822描述了电子邮件地址格式规则,因此在internet上搜索“RFC822电子邮件regex”似乎很有用。

For Perl the answer seems to be easy: use Mail::RFC822::Address: regexp-based address validation

对于Perl来说,答案似乎很简单:使用Mail:::RFC822::Address:基于正则表达式的地址验证

RFC 822 Email Address Parser in PHP

RFC 822电子邮件地址解析器。

Thus, to achieve the most correct handling of email addresses, one should either locate the most precise regex that there is out somewhere for the particular toolkit (ATL in your case) or - in case there's no suitable existing regex yet - adapt a very precise regex of another toolkit (Perl above seems to be a very complete albeit difficult candidate).

因此,要实现最正确的处理的电子邮件地址,一个应该定位最准确的正则表达式,为特定地方工具包(ATL)或——如果还没有合适的现有的正则表达式——适应另一个工具包的一个非常精确的正则表达式(Perl上似乎是一个非常完整的尽管困难的候选人)。

If you're trying to match a specific sub part of email addresses (as seems to be the case given your question), then it probably still makes sense to start with the most up-to-date/correct/universal regex and specifically limit it to the parts that you require.

如果您正在尝试匹配电子邮件地址的特定子部分(就像您的问题所显示的那样),那么从最新的/正确的/通用的regex开始,并特别将其限制在您需要的部分可能仍然是有意义的。

Perhaps I stated the obvious, but I hope it helped.

也许我说的很明显,但我希望它能有所帮助。

#1


2  

Try: ^[a-zA-Z0-9\._%\+\-]+@([a-zA-Z0-9-]+\.)+\c\c\c?\c?$

试题:^[a-zA-Z0-9 \ ._ % \ + \ -]+ @([a-zA-Z0-9 -]+ \)+ \ \ c \ c ?美元\ c ?

This expression replaces the [A-Z]{2,4} sequence which CAtlRegExp doesn't support with \c\c\c?\c?

这个表达式替代了CAtlRegExp不支持的[A-Z]{2,4}序列。

\c serves as an abbreviation of [a-zA-Z]. The question marks after the 3rd and 4th \c's indicate they can match either zero or one characters. As a result, this portion of the expression matches 2, 3 or 4 characters, but neither more nor less.

\c是[a-zA-Z]的缩写。第三和第四\c后面的问号表示它们可以匹配零或一个字符。因此,表达式的这一部分匹配2、3或4个字符,但不会多或少。

#2


1  

You are trying to match email addresses, a very widely used critical element of internet communication.

您正在尝试匹配电子邮件地址,一个非常广泛使用的关键因素,互联网通信。

To which I would say that this job is best done with the most widely used most correct regex.

对此,我认为最好使用最广泛使用的最正确的regex来完成这项工作。

Since email address format rules are described by RFC822, it seems useful to do internet searches for something like "RFC822 email regex".

由于RFC822描述了电子邮件地址格式规则,因此在internet上搜索“RFC822电子邮件regex”似乎很有用。

For Perl the answer seems to be easy: use Mail::RFC822::Address: regexp-based address validation

对于Perl来说,答案似乎很简单:使用Mail:::RFC822::Address:基于正则表达式的地址验证

RFC 822 Email Address Parser in PHP

RFC 822电子邮件地址解析器。

Thus, to achieve the most correct handling of email addresses, one should either locate the most precise regex that there is out somewhere for the particular toolkit (ATL in your case) or - in case there's no suitable existing regex yet - adapt a very precise regex of another toolkit (Perl above seems to be a very complete albeit difficult candidate).

因此,要实现最正确的处理的电子邮件地址,一个应该定位最准确的正则表达式,为特定地方工具包(ATL)或——如果还没有合适的现有的正则表达式——适应另一个工具包的一个非常精确的正则表达式(Perl上似乎是一个非常完整的尽管困难的候选人)。

If you're trying to match a specific sub part of email addresses (as seems to be the case given your question), then it probably still makes sense to start with the most up-to-date/correct/universal regex and specifically limit it to the parts that you require.

如果您正在尝试匹配电子邮件地址的特定子部分(就像您的问题所显示的那样),那么从最新的/正确的/通用的regex开始,并特别将其限制在您需要的部分可能仍然是有意义的。

Perhaps I stated the obvious, but I hope it helped.

也许我说的很明显,但我希望它能有所帮助。