Greetings,

how do I create a regulare expression that isolates the e-mail address (and
places it into another string variable) in the following input line (read in
from a text file):

From: "Shmoe, Joe" <joe.shmoe@work.com>

Thanks

Re: regular expression to extract e-mail address by Al

Al
Sun Jan 22 12:42:22 CST 2006


"sneaky" <sneaky@the.riverbank> wrote in message
news:uJH86e3HGHA.2444@TK2MSFTNGP11.phx.gbl...
> Greetings,
>
> how do I create a regulare expression that isolates the e-mail address
> (and
> places it into another string variable) in the following input line (read
> in
> from a text file):
>
> From: "Shmoe, Joe" <joe.shmoe@work.com>


fulladdr = "From: ""Shmoe, Joe"" <joe.shmoe@work.com>"
smtp = split( fulladdr&"<", "<" )(1)
smtp = split( smtp&">", ">" )(0)
wscript.echo "smtp address: " & smtp

this will, of course, only yield an smtp address if the original string
includes one contained in angle brackets.

/Al



Re: regular expression to extract e-mail address by McKirahan

McKirahan
Sun Jan 22 12:15:55 CST 2006

"sneaky" <sneaky@the.riverbank> wrote in message
news:uJH86e3HGHA.2444@TK2MSFTNGP11.phx.gbl...
> Greetings,
>
> how do I create a regulare expression that isolates the e-mail address
(and
> places it into another string variable) in the following input line (read
in
> from a text file):
>
> From: "Shmoe, Joe" <joe.shmoe@work.com>

Presuming that the e-mail address is enclosed with "<" and ">";
here's a solution that doesn't use a regular expression:

Option Explicit
Dim at, ea, i0, i1, i2
at = "From: ""Shmoe, Joe"" <joe.shmoe@work.com>"
i0 = InStr(at,"@")
If i0 > 0 Then
i1 = InStrRev(at,"<",i0)
i2 = InStr(i0,at,">")
ea = Mid(at,i1+1,i2-i1-1)
End If
WScript.Echo ea



Re: regular expression to extract e-mail address by James

James
Sun Jan 22 13:34:14 CST 2006

"sneaky" <sneaky@the.riverbank> wrote in message
news:uJH86e3HGHA.2444@TK2MSFTNGP11.phx.gbl...
> Greetings,
>
> how do I create a regulare expression that isolates the e-mail address
(and
> places it into another string variable) in the following input line (read
in
> from a text file):
>
> From: "Shmoe, Joe" <joe.shmoe@work.com>

Give this a try and see if it does what you want:

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dim oRegEx, sLine, sEmail

Set oRegEx = CreateObject("VBScript.RegExp")

oRegEx.Pattern = "[\w!#$%&'*+-./=?^`{|}~]+@[\w-]+\.[A-Za-z]+"

sLine = "From: ""Shmoe, Joe"" <joe.shmoe@work.com>"

Select Case oRegEx.Test(sLine)
Case True sEmail = oRegEx.Execute(sLine)(0)
Case Else sEmail = "No valid email address found."
End Select

MsgBox sEmail
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




Re: regular expression to extract e-mail address by Dr

Dr
Sun Jan 22 17:29:21 CST 2006

JRS: In article <0-6dncExec5BVk7eRVn-oQ@comcast.com>, dated Sun, 22 Jan
2006 12:17:39 remote, seen in news:microsoft.public.scripting.vbscript,
McKirahan <News@McKirahan.com> posted :
>"sneaky" <sneaky@the.riverbank> wrote in message
>news:uJH86e3HGHA.2444@TK2MSFTNGP11.phx.gbl...

>> how do I create a regulare expression that isolates the e-mail address
>(and
>> places it into another string variable) in the following input line (read
>in
>> from a text file):
>>
>> From: "Shmoe, Joe" <joe.shmoe@work.com>
>
>Presuming that the e-mail address is enclosed with "<" and ">";
>here's a solution that doesn't use a regular expression:
>
>Option Explicit
>Dim at, ea, i0, i1, i2
> at = "From: ""Shmoe, Joe"" <joe.shmoe@work.com>"
> i0 = InStr(at,"@")
>If i0 > 0 Then
> i1 = InStrRev(at,"<",i0)
> i2 = InStr(i0,at,">")
> ea = Mid(at,i1+1,i2-i1-1)
>End If
>WScript.Echo ea

Tell me, which part of that is the regular expression that the OP asked
for? If, as seems likely, this is a class-work question, he'll need to
know.

A regular expression will not do what the OP asks; but it can be used as
part of code that will.

Here's an example that only needs to be translated to VBS if a VBS
solution is necessary :

S = F.X0.value.match(/(<)(.*)(>)/)[2]

--
© John Stockton, Surrey, UK. ???@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.

Re: regular expression to extract e-mail address by Dr

Dr
Mon Jan 23 11:54:57 CST 2006

JRS: In article <#gsBVr4HGHA.516@TK2MSFTNGP15.phx.gbl>, dated Sun, 22
Jan 2006 13:34:14 remote, seen in news:microsoft.public.scripting.vbscri
pt, James Whitlow <jwhitlow@letter.com> posted :
>"sneaky" <sneaky@the.riverbank> wrote in message
>news:uJH86e3HGHA.2444@TK2MSFTNGP11.phx.gbl...

>> From: "Shmoe, Joe" <joe.shmoe@work.com>

>oRegEx.Pattern = "[\w!#$%&'*+-./=?^`{|}~]+@[\w-]+\.[A-Za-z]+"

Does that correctly accept ALL E-mail addresses that are permitted by
the RFCs? Mine, for example? I think that it does not; it does not in
javascript.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Re: regular expression to extract e-mail address by James

James
Mon Jan 23 13:33:18 CST 2006

"Dr John Stockton" <jrs@merlyn.demon.co.uk> wrote in message
news:04DxiWCxhR1DFwrf@merlyn.demon.co.uk...
> JRS: In article <#gsBVr4HGHA.516@TK2MSFTNGP15.phx.gbl>, dated Sun, 22
> Jan 2006 13:34:14 remote, seen in news:microsoft.public.scripting.vbscri
> pt, James Whitlow <jwhitlow@letter.com> posted :
> >"sneaky" <sneaky@the.riverbank> wrote in message
> >news:uJH86e3HGHA.2444@TK2MSFTNGP11.phx.gbl...
>
> >> From: "Shmoe, Joe" <joe.shmoe@work.com>
>
> >oRegEx.Pattern = "[\w!#$%&'*+-./=?^`{|}~]+@[\w-]+\.[A-Za-z]+"
>
> Does that correctly accept ALL E-mail addresses that are permitted by
> the RFCs? Mine, for example? I think that it does not; it does not in
> javascript.

I did not intend on making this the perfect RE pattern for email
addresses. I wanted to try and cover the original example posted by the OP
(and go a little beyond). I briefly looked at the RFC, but I did not want to
spend hours it and conducting extensive testing. I did not take addresses
with multiple periods to the right of the @ symbol into consideration.

Give this pattern a try and see if it is more to your liking:

oRegEx.Pattern = "[\w!#$%&'*+-./=?^`{|}~]+@[\w-.]+"

If you think of any other examples that would trip up the above pattern,
post them here and I will try and modify the pattern to correctly identify
them.



Re: regular expression to extract e-mail address by Michael

Michael
Mon Jan 23 18:27:18 CST 2006

> ... I briefly looked at the RFC, but I
> did not want to spend hours it and conducting extensive testing...

As an FYI, the regular expression book I learned from years ago ("Mastering
Regular Expressions", an O'Reilly classic) had such an email address regular
expression implementation and it was over 4000 charaters long!!!



--
Michael Harris
Microsoft MVP Scripting





Re: regular expression to extract e-mail address by Al

Al
Mon Jan 23 19:57:58 CST 2006


"Michael Harris (MVP)" <mikhar at mvps dot org> wrote in message
news:O7q0qzHIGHA.2212@TK2MSFTNGP15.phx.gbl...
>> ... I briefly looked at the RFC, but I
>> did not want to spend hours it and conducting extensive testing...
>
> As an FYI, the regular expression book I learned from years ago
> ("Mastering Regular Expressions", an O'Reilly classic) had such an email
> address regular expression implementation and it was over 4000 charaters
> long!!!

And not only that, but even the most detailed and exacting code will be
unable to easily differentiate between an e-mail address that exists and one
that does not.

/Al



Re: regular expression to extract e-mail address by Dr

Dr
Tue Jan 24 12:00:19 CST 2006

JRS: In article <uCmAfPFIGHA.3460@TK2MSFTNGP12.phx.gbl>, dated Mon, 23
Jan 2006 13:33:18 remote, seen in news:microsoft.public.scripting.vbscri
pt, James Whitlow <jameswhitlow@spamcop.net> posted :
>"Dr John Stockton" <jrs@merlyn.demon.co.uk> wrote in message
>news:04DxiWCxhR1DFwrf@merlyn.demon.co.uk...
>> JRS: In article <#gsBVr4HGHA.516@TK2MSFTNGP15.phx.gbl>, dated Sun, 22
>> Jan 2006 13:34:14 remote, seen in news:microsoft.public.scripting.vbscri
>> pt, James Whitlow <jwhitlow@letter.com> posted :
>> >"sneaky" <sneaky@the.riverbank> wrote in message
>> >news:uJH86e3HGHA.2444@TK2MSFTNGP11.phx.gbl...
>>
>> >> From: "Shmoe, Joe" <joe.shmoe@work.com>

> Give this pattern a try and see if it is more to your liking:
>
>oRegEx.Pattern = "[\w!#$%&'*+-./=?^`{|}~]+@[\w-.]+"
>
> If you think of any other examples that would trip up the above pattern,
>post them here and I will try and modify the pattern to correctly identify
>them.

That accepts my address.

But it also accepts jj@aa.bb..cc.jjj so it is not a validator -
which is harmless, since the only real test of an E-address is whether
it gets the mail to the right person. It also accepts .@. .

The OP's prime need, AFAICS, is to get whatever is within the denoters
< > and not to wonder about its validity; no more checking is needed, as
there should be no other < > present (but one could test for .+@.+\..+
in <> to guard against < > being characters in the recipient's language
- "jo<ej>im" <jj@aa.bb> should I think be valid.

And one might require the <jj@aa.bb> to be at the end of the line.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

Re: regular expression to extract e-mail address by Dr

Dr
Tue Jan 24 17:36:15 CST 2006

JRS: In article <O8lsamIIGHA.376@TK2MSFTNGP12.phx.gbl>, dated Mon, 23
Jan 2006 17:57:58 remote, seen in news:microsoft.public.scripting.vbscri
pt, Al Dunbar <AlanNOSPAmDrub@hotmail.com> posted :
>
>"Michael Harris (MVP)" <mikhar at mvps dot org> wrote in message
>news:O7q0qzHIGHA.2212@TK2MSFTNGP15.phx.gbl...
>>> ... I briefly looked at the RFC, but I
>>> did not want to spend hours it and conducting extensive testing...
>>
>> As an FYI, the regular expression book I learned from years ago
>> ("Mastering Regular Expressions", an O'Reilly classic) had such an email
>> address regular expression implementation and it was over 4000 charaters
>> long!!!
>
>And not only that, but even the most detailed and exacting code will be
>unable to easily differentiate between an e-mail address that exists and one
>that does not.

You oversimplify the task.

I'm a dial-up user with my own dotted quad etc.; I can create and
destroy E-mail addresses while not connected to the Internet. Code on
other machines can only observe (sort of) the state whilst I am
connected.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

Re: regular expression to extract e-mail address by Al

Al
Tue Jan 24 19:05:29 CST 2006


"Dr John Stockton" <jrs@merlyn.demon.co.uk> wrote in message
news:SjkIUXEvnr1DFwPE@merlyn.demon.co.uk...
> JRS: In article <O8lsamIIGHA.376@TK2MSFTNGP12.phx.gbl>, dated Mon, 23
> Jan 2006 17:57:58 remote, seen in news:microsoft.public.scripting.vbscri
> pt, Al Dunbar <AlanNOSPAmDrub@hotmail.com> posted :
>>
>>"Michael Harris (MVP)" <mikhar at mvps dot org> wrote in message
>>news:O7q0qzHIGHA.2212@TK2MSFTNGP15.phx.gbl...
>>>> ... I briefly looked at the RFC, but I
>>>> did not want to spend hours it and conducting extensive testing...
>>>
>>> As an FYI, the regular expression book I learned from years ago
>>> ("Mastering Regular Expressions", an O'Reilly classic) had such an email
>>> address regular expression implementation and it was over 4000 charaters
>>> long!!!
>>
>>And not only that, but even the most detailed and exacting code will be
>>unable to easily differentiate between an e-mail address that exists and
>>one
>>that does not.
>
> You oversimplify the task.

Not really, that was just a bit of irony, included for the purpose of
humour...

If one actually *could* reliably validate tentative smtp addresses as
representing actual existing email addesses, the spam situation would be
quite different for both the spammers and the spammees.

/Al



Re: regular expression to extract e-mail address by Al

Al
Tue Jan 24 19:09:42 CST 2006


"Dr John Stockton" <jrs@merlyn.demon.co.uk> wrote in message
news:lvTFnmDzsm1DFw48@merlyn.demon.co.uk...
> JRS: In article <uCmAfPFIGHA.3460@TK2MSFTNGP12.phx.gbl>, dated Mon, 23
> Jan 2006 13:33:18 remote, seen in news:microsoft.public.scripting.vbscri
> pt, James Whitlow <jameswhitlow@spamcop.net> posted :
>>"Dr John Stockton" <jrs@merlyn.demon.co.uk> wrote in message
>>news:04DxiWCxhR1DFwrf@merlyn.demon.co.uk...
>>> JRS: In article <#gsBVr4HGHA.516@TK2MSFTNGP15.phx.gbl>, dated Sun, 22
>>> Jan 2006 13:34:14 remote, seen in news:microsoft.public.scripting.vbscri
>>> pt, James Whitlow <jwhitlow@letter.com> posted :
>>> >"sneaky" <sneaky@the.riverbank> wrote in message
>>> >news:uJH86e3HGHA.2444@TK2MSFTNGP11.phx.gbl...
>>>
>>> >> From: "Shmoe, Joe" <joe.shmoe@work.com>
>
>> Give this pattern a try and see if it is more to your liking:
>>
>>oRegEx.Pattern = "[\w!#$%&'*+-./=?^`{|}~]+@[\w-.]+"
>>
>> If you think of any other examples that would trip up the above pattern,
>>post them here and I will try and modify the pattern to correctly identify
>>them.
>
> That accepts my address.
>
> But it also accepts jj@aa.bb..cc.jjj so it is not a validator -
> which is harmless, since the only real test of an E-address is whether
> it gets the mail to the right person. It also accepts .@. .
>
> The OP's prime need, AFAICS, is to get whatever is within the denoters
> < > and not to wonder about its validity; no more checking is needed, as
> there should be no other < > present (but one could test for .+@.+\..+
> in <> to guard against < > being characters in the recipient's language
> - "jo<ej>im" <jj@aa.bb> should I think be valid.
>
> And one might require the <jj@aa.bb> to be at the end of the line.

I agree with you. My solution simply extracted what was contained within the
angle brackets.

In fact it may not be possible to determine what approach is best for the OP
in this case without knowing where the data is coming from. If entered
interactively, it is highly likely that much of the input will be totally
invalid.

If, on the other hand, the tentative email address is constrained to be
picked from a list of known valid addresses, that is quite another thing.

/Al