我是VB语言的初学者,我有一个格式的字符串:
史密斯,亚历克斯(asmith@gmail.com)
我试图从这个字符串中分离出“asmith”。我假设我们必须在“(”然后在“@”处拆分并获得第一个指数?
感谢任何帮助。
答案 0 :(得分:4)
我假设我们必须在"("然后在" @"并获得第一个索引?)
你的方法是正确的。您的算法到VB.NET的字面翻译将是:
Dim firstPartOfMailAddress = text.Split("("c)(1).Split("@"c)(0)
说明:
"("c
是字符文字。拆分需要一个字符,而不是一个字符串(可能是"("
)。(...)
运算符进行索引。因此,您将获取第一个拆分的第二部分(1)
和第二个拆分的第一部分(0)
。注意:
(
,则会失败。答案 1 :(得分:2)
以下是您的选择:
Dim input = "Smith, Alex (asmith@gmail.com)"
Dim output= input.Split("("c, "@"c)(1)
答案 2 :(得分:1)
使用class:
Imports System.Globalization
Imports System.Text.RegularExpressions
Public Class RegexUtilities
Dim invalid As Boolean = False
Public Function IsValidEmail(strIn As String) As Boolean
invalid = False
If String.IsNullOrEmpty(strIn) Then Return False
' Use IdnMapping class to convert Unicode domain names.
Try
strIn = Regex.Replace(strIn, "(@)(.+)$", AddressOf Me.DomainMapper,
RegexOptions.None, TimeSpan.FromMilliseconds(200))
Catch e As RegexMatchTimeoutException
Return False
End Try
If invalid Then Return False
' Return true if strIn is in valid e-mail format.
Try
Return Regex.IsMatch(strIn,
"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250))
Catch e As RegexMatchTimeoutException
Return False
End Try
End Function
Private Function DomainMapper(match As Match) As String
' IdnMapping class with default property values.
Dim idn As New IdnMapping()
Dim domainName As String = match.Groups(2).Value
Try
domainName = idn.GetAscii(domainName)
Catch e As ArgumentException
invalid = True
End Try
Return match.Groups(1).Value + domainName
End Function
End Class
并在表单和customzition中使用代码:
Dim util As New RegexUtilities()
Dim emailAddresses() As String = {"david.jones@proseware.com", "d.j@server1.proseware.com",
"jones@ms1.proseware.com", "j.@server1.proseware.com",
"j@proseware.com9", "js#internal@proseware.com",
"j_9@[129.126.118.1]", "j..s@proseware.com",
"js*@proseware.com", "js@proseware..com",
"js@proseware.com9", "j.s@server1.proseware.com",
"""j\""s\""""@proseware.com", "js@contoso.中国"}
For Each emailAddress As String In emailAddresses
If util.IsValidEmail(emailAddress) Then
Console.WriteLine("Valid: {0}", emailAddress)
Else
Console.WriteLine("Invalid: {0}", emailAddress)
End If
Next