从文本框中删除除电子邮件地址之外的所有内容

发布于 2024-11-04 01:38:25 字数 429 浏览 2 评论 0原文

我在 Access Db 中有一个文本框,用户应在其中输入电子邮件地址。这是通过从 Outlook 2010 进行复制来完成的。不幸的是,该副本还带有名称和尖括号,Fred Bloggs <[email protected]>

我希望使用此正则表达式

.pattern = "[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}"

来识别电子邮件地址。

如果没有输入有效的电子邮件地址,我将如何删除其他所有内容并引发错误?

I've a text box in an Access Db that the user should enter an email address into. This is done by copying from Outlook 2010. Unfortunately, that copy also brings the Name and angular brackets, Fred Bloggs <[email protected]>

I was hoping to use this regex

.pattern = "[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}"

to identify the email address.

How would I strip everything else and throw an error if no valid email address is entered?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯下孤影 2024-11-11 01:38:25

Microsoft Access VBA 没有内置的正则表达式引擎。不过,您可以添加对 VBScript 正则表达式库的引用并使用该库。

以下是解释如何执行此操作的相关问题:

Microsoft Access VBA does not have a built-in Regex engine. You can, however, add a reference to the VBScript Regular Expression Library and use that one.

Here's a related question that explains how to do that:

梦途 2024-11-11 01:38:25

如果电子邮件始终包含在最终的<> 中,您可以

Public Function fmt(email As String) As String
pos = InStrRev(email, "<")
If (pos > 0) Then
   email = Mid$(email, 1 + pos, 1 + Len(email) - pos)
   email = Left$(email, Len(email) - 1)
End If
fmt = email
End Function

replace(mid(email,instrrev(email,"<")+1,len(email)),">","")

编辑;

对于正则表达式检查,添加对“Microsoft VBScript 正则表达式库”的引用(工具 > 引用);

Public Function fmt(email As String) As String
pos = InStrRev(email, "<")
If (pos > 0) Then
   email = Mid$(email, 1 + pos, 1 + Len(email) - pos)
   email = Left$(email, Len(email) - 1)
End If
fmt = email
With New RegExp
    .Global = True
    .IgnoreCase = True
    .MultiLine = True
    .Pattern = "^\S+@\S+\.\S+$"
    If Not .Test(fmt) Then fmt = ""
End With
End Function

这将返回一个有效的电子邮件地址,如果无效则返回“”。

我把你的 RE 掉了;推理:使用正则表达式验证电子邮件地址< /a>

If the email is always enclosed in the final <> you could

Public Function fmt(email As String) As String
pos = InStrRev(email, "<")
If (pos > 0) Then
   email = Mid$(email, 1 + pos, 1 + Len(email) - pos)
   email = Left$(email, Len(email) - 1)
End If
fmt = email
End Function

or

replace(mid(email,instrrev(email,"<")+1,len(email)),">","")

Edit;

For a regexp check add a reference to the "Microsoft VBScript Regular Expressions library" (tools > references) and;

Public Function fmt(email As String) As String
pos = InStrRev(email, "<")
If (pos > 0) Then
   email = Mid$(email, 1 + pos, 1 + Len(email) - pos)
   email = Left$(email, Len(email) - 1)
End If
fmt = email
With New RegExp
    .Global = True
    .IgnoreCase = True
    .MultiLine = True
    .Pattern = "^\S+@\S+\.\S+$"
    If Not .Test(fmt) Then fmt = ""
End With
End Function

This returns a valid email address, or "" if its not valid.

I dropped your RE; reasoning: Using a regular expression to validate an email address

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文