dlofrodloh
dlofrodloh

Reputation: 1744

How to find a phrase in a string based on a character

Let's say I have the following string:

sdfhahsdfu^asdhfhasdf^asd7f8asdfh^asdfhasdf^[email protected]^asdhfausdf^asodfuasdufh^alsdfhasdh

What's the best way of extracting the email from that string? I thought of maybe split(string, "@") but then I'm not sure where to go from there.

Note: the email will always be flanked by ^ on either side, but the position in the string will be different depending on the string.

Upvotes: 1

Views: 68

Answers (2)

Hrqls
Hrqls

Reputation: 2951

I would split over ^ and then loop through all items to find something containing a @

'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^[email protected]^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

Upvotes: 1

Racil Hilan
Racil Hilan

Reputation: 25371

You can use Regex to find your string. Try something like:

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

Upvotes: 4

Related Questions