Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strip email address from string... 2

Status
Not open for further replies.

hererxnl

Technical User
Jul 8, 2003
239
US

It may be that this is best solved with regular expressions, of which I know almost nothing (ready to learn though!). I'm pulling attachments from users emails and want to create a directory for their email address which I have stored in a string.

For the sake so simplicity I'll say its:
Code:
strEmail = Item.From
strEmail will generally return something like:
"John Doe" <johndoe@noemail.com>

How can I strip out the "johndoe@noemail.com"?

Ideas?? Thanks in advance, as always.

 
Will there always be quotes around the name? If so...

dim strTest as string
dim strName as string

strTest = """John Doe"" <JohnDoe@noemail.com>"
strname = split(strTest, """")(1)



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Or

strName Split(strTest, "<")(0)

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 

George, thanks for the quick response. Unfortunately probably not. That's why I began to think this is a case for regular expressions. However, that does give me some new avenues to explore. thanks.

Originally I thought of finding the position of the @ symbol instr and branching out left and right until I found an illegal character, but it's the end of the day and the brain needs to unfreeze first.
 
Yep. This is ideal for Regular Expressions. And we've covered it ... thread222-567774
 
Using DrJavaJoe's post in thread222-5677743 as a starting point, here is a RE soultion...

Code:
Option Explicit
Private Sub Command1_Click()
    MsgBox StripEmailAddress("""John Doe"" <johndoe@noemail.com>")
End Sub

Public Function StripEmailAddress(ByVal strExp As String) As String
    Dim MyRegExp As RegExp
    Set MyRegExp = New RegExp
    MyRegExp.Pattern = "<{0,1}(\ )*[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z](\ )*\>{0,1}"
    StripEmailAddress = MyRegExp.Replace(strExp, "")
End Function
 
My thread reference should be the same as strongm's. I must have started on this just right before you posted, strongm.
 

Thanks so much guys. I'm gonna get on it now.

 

Well I'm back to it this morning (EST). My connect last night didn't allow appropraite testing of the RE that was offered up. Not nowing much about RE I didn't realize that the code that was posted did exactly the opposite of what I'm needing, which is probably my fault for the wording of the subject of the post.

I was actually trying to discard the User's name, thus strip out the emaill address. Looking back I got what I asked for. I've been playing with this and reading up on RE but I'm still having no luck getting just the email.

Can anyone help. Hey, another star's up for grabs. :)

Thanks for the patience.

 
I don't know about Regular expressions (obviously). One day I will sit down and start learning it. However, I can still offer a good suggestion.

If you use RE to 'get the email address', you could take the original string, and replace the results of the RE with nothing.

StringWithoutEmail = Replace(OriginalString, ResultsOfRegularExpression, "")

There may be a Regular Expression method of removing the Email Address, but being unfamiliar with it, this is the best advice I can give.

Good Luck

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 

That might not be efficient, but it could be an answer. Thanks George, I'm going to play with idea idea bit now.



 
>>There may be a Regular Expression method of removing the Email Address, but being unfamiliar with it, this is the best advice I can give.

George, that is exactly what my code does!

hererxnl, if you use the code from the referenced link (the one by strongm that I made an attempt at referencing) then you will have your solution there. No need for any replace function.

The basic difference lies in the pattern and then how you execute the pattern.

Here is a function that demostrates the difference (both in the pattern and how it is executed). This should be what you're looking for. I apologize for the misunderstanding.

Code:
Public Function ReturnEmailAddress(ByVal strExp As String) As String
    Dim MyRegExp As RegExp
    Dim Rslt As MatchCollection
        
    Set MyRegExp = New RegExp
    
    MyRegExp.Pattern = "[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]"
    If MyRegExp.Test(strExp) Then
        ReturnEmailAddress = MyRegExp.Execute(strExp)(0)
    Else
        ReturnEmailAddress = ""
    End If
End Function

 

This answer kind of defeats the purpose as it's using the vb Replace function, but it does work:
Code:
Public Function StripEmailAddress(ByVal strExp As String) As String
    Dim MyRegExp As RegExp
    Set MyRegExp = New RegExp
    MyRegExp.Pattern = """{0,1}(\ )*[a-zA-Z][\w\.-]*[a-zA-Z0-9] [a-zA-Z][\w\.-]*[a-zA-Z0-9](\ )*\""{0,1}"
    StripEmailAddress = MyRegExp.Replace(strExp, "")
    StripEmailAddress = Replace(StripEmailAddress, "<", "")
    StripEmailAddress = Replace(StripEmailAddress, ">", "")
End Function
It takes the string:
"John Doe" <johndoe@noemail.com>

And Returns:
johndoe@noemail.com

Can someone povide the RE answer for removing the < and > symbols in a string so I can stay away from vb Replace?
 

Posted while you did bjd4jc. Thank again for the hand holding. I think I'm starting to get it. Now I'm going back to the prog with your adapted function.

No need to apologize for MY miscommunication. You've been a big help.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top