Android Question Find Email Address In a string?

aidymp

Well-Known Member
Licensed User
Longtime User
Hi Im struggling a bit with this, basically I want to find an email address in a message body
i have played with regex but thats not what is needed! As basically I want to search for an Email address in a string not validate one!

I found this VB.net code here http://www.freevbcode.com/ShowCode.asp?ID=9434 and it sounds perfect but i cant convert it

B4X:
Public Function FindEMailAddressesInStr(ByVal StringWithEmails As String) As List(Of String)
    Dim emailList As New List(Of String)
    Dim RegExMatch As Text.RegularExpressions.MatchCollection = _
        System.Text.RegularExpressions.Regex.Matches(StringWithEmails, _
        "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")

    For i As Integer = 0 To RegExMatch.Count - 1
        If emailList.Contains(RegExMatch(i).Value) = False Then
            emailList.Add(RegExMatch(i).Value)
        End If
    Next

    Return emailList
End Function

Any Help

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Log(FindEmails($"[email protected], lkjlk jsdf lksdjf
   [email protected]"$))
End Sub

Sub FindEmails(str As String) As List
   Dim m As Matcher = Regex.Matcher("\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", str)
   Dim res As List
   res.Initialize
   Do While m.Find
     res.Add(m.Match)
   Loop
   Return res
End Sub
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Try THIS

Also, search for Regex.Matcher.

Thanks but from what i read that just validates if a sting contains a correctly formatted email address? not actually finds an email address in a large text file? But
Thanks for your very fast response! ;)
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Here:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Log(FindEmails($"[email protected], lkjlk jsdf lksdjf
   [email protected]"$))
End Sub

Sub FindEmails(str As String) As List
   Dim m As Matcher = Regex.Matcher("\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", str)
   Dim res As List
   res.Initialize
   Do While m.Find
     res.Add(m.Match)
   Loop
   Return res
End Sub


Thanks once again Erel, Amazing! , Amazing! , Amazing! ;)
 
Upvote 0
Top