如何根据字符在字符串中查找短语

时间:2014-03-24 00:51:04

标签: vb.net visual-studio vb6

假设我有以下字符串:

sdfhahsdfu ^ asdhfhasdf ^ asd7f8asdfh ^ asdfhasdf ^的 testemail@email.com ^ asdhfausdf ^ asodfuasdufh ^ alsdfhasdh

从该字符串中提取电子邮件的最佳方法是什么?我想也许分裂(字符串,“@”),但后来我不知道从那里去哪里。

注意:电子邮件的两侧始终都是^,但字符串中的位置会因字符串而异。

2 个答案:

答案 0 :(得分:4)

您可以使用Regex查找字符串。尝试类似:

System.Text.RegularExpressions.Regex.Match("\^[^\^]+@[^\^]+\^", myString)

答案 1 :(得分:1)

我会拆分^然后遍历所有项目以找到包含@

的内容
'1 form with:
'  1 command button: name=Command1
Option Explicit

Private Sub Command1_Click()
  Dim lngItem As Long
  Dim strString As String
  Dim strItem() As String
  strString = "sdfhahsdfu^asdhfhasdf^asd7f8asdfh^asdfhasdf^testemail@email.com^asdhfausdf^asodfuasdufh^alsdfhasdh"
  strItem = Split(strString, "^")
  For lngItem = 0 To UBound(strItem)
    If InStr(strItem(lngItem), "@") > 0 Then
      DoEmail strItem(lngItem)
    End If
  Next lngItem
End Sub

Private Sub DoEmail(strEmail As String)
  Print strEmail
End Sub