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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Maybe use mid function? to extract data 1

Status
Not open for further replies.

benniesanders

Programmer
Jan 20, 2002
199
US
Greetings,

I have 300 records, each with a block of text I need to extract an email address from. Every email address has a space before it and a space after it and of course the @ between.

So how would I go about extracting everything to the left of the @ and everything to the right of the @ after it finds a space on each end? I'm probably making this much more complicated than necessary, right? Many thanks in advance for any ideas.
 
What language are you doing your ASP in?

VBScript?
Javascript?

[small]"King Coleman, live foreva!! Yeah buddy, light weight!!!"[/small]
<.
 
The way I can think to do it would involve looping through each block of text in each record and do a Split on that block.

Split will force that block of text into an array split at spaces.

Then do a loop through the array looking for the value in the array that contains a '@'. This would be done using the InStr function.

Code:
dim textBlock, arr, emailAdd
// Grab record from recordset and get the block of text containing the email address
textBlock = oRS("texBlockField");
arr = Split(textBlock)
For each a in arr
   if (Instr(arr(a), "@") > 0) then 
      a = arr.length
      emailAdd = arr(a);
   endIf
Next 
//Do what you need to with the email address from the last record

//loop and grab next recordset
oRS.MoveNext;

*Note: the syntax may not be perfect, I'm from a Javascript
background, but this will work.

You may have to parse some string values too.





[small]"Mom........MEATLOAF!!!! F***!!!!"[/small]
<.
 
try this:

Code:
<%
dim email, username, firmname'
email = " myname@myfirm.com "
email = trim(email)
username = Mid(email, 1, instr(email, "@")-1)
firmname = Mid(email, instr(email, "@")+1)

response.write username & "<br>"
response.write firmname
%>

just putting it simple for you to understand...

-DNG
 
Thank you both so much. I will let you know how everything turns out. It's important I figure out a way to extract these. We provide medical training courses and mail course lists. As you can imagine, we have bounces. I've figured out how to extract the bounces with email addresses between [] and <> but not "stand alones". Many thanks again.
 
Some functions to do this from my page analysis tools

Code:
<%

function CountMailAddress(strIn)
'return the positions for each "@" in a string
dim SpcMatches
dim objRE 
dim z,i,temp
set objRE = New RegExp
objRE.global = true
objRE.ignorecase = True
objRE.pattern = "@"
set SpcMatches = objRE.execute(strIN)
redim temp(SpcMatches.count)
i = 0
for each z in SpcMatches 
temp(i) = z.firstindex + 1 
i = i + 1
next
CountMailAddress = temp
set SpcMatches = nothing
set objRE = nothing
end function

function GetName(strIn,intPos)
' extract a email username from string
dim startPos
startPos = InStrRev(strIn," ",intPos)
GetName = mid(strIn, startPos,intPos - startPos)
end function

function GetHostname(strIn,intPos)
' extract a email hostname from string
dim EndPos
EndPos = InStr(intPos,strIn," ")
GetHostname = mid(strIn, intPos + 1, endPos - intPos)
end function
%>

Demo of how to use the functions
Code:
<%
dim iMailMatch 
dim testString
teststring = "Some text me@here.com more text someone@there.co.uk more text"
 

iMailMatch = CountMailAddress(testString)
for i = 0 to ubound(iMailMatch ) -1 
response.write GetName(teststring,iMailMatch (i))
response.write " at " 
response.write GetHostName(teststring,iMailMatch (i))
response.write "<br>"
next
%>

works like an harvester

Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
guys, I can't get any of this to work. I'm not very good at the split thing, and Monk, I'm getting an error on this:

Code:
if (Instr(arr(a), "@") > 0) then 
      a = arr.length
      emailAdd = arr(a);

I'm not sure DoTNetGnat about yours. Am I supposed to run that after his? I"m totally lost here. Sorry so dense...
 
AHHH yeah
on

Code:
a = arr.length

That is Javascript, I'll lookup the VBScript syntax.

[small]"Mom........MEATLOAF!!!! F***!!!!"[/small]
<.
 
Change

a = arr.length to

Code:
a = UBound(arr)

[small]"Mom........MEATLOAF!!!! F***!!!!"[/small]
<.
 
Wow, thanks Chris. I was posting to the others when I found yours! I tried it and got:

Invalid procedure call or argument: 'mid'

on this line

GetHostname = mid(strIn, intPos + 1, endPos - intPos)

and of course I'm not smart enough to know what to do at this point. Many many thanks.
 
that can happen if the string passed to the function has a match at the very end of the string and the TLD is the last characters of the string.

the quick workaround is to add a space to the string before passing it.

in the example it would be

teststring = "whatever"
teststring = teststring & " "




Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
Chris, brilliant. I need to do a teeny bit of tweaking, but not much. many thanks for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top