Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Foxpro - finding all email addresses in string with Regex

Posted on 2013-11-04
4
Medium Priority
?
1,442 Views
Last Modified: 2013-11-04
How do I find multiple email addresses in a string using Foxpro and Regex?
0
Comment
Question by:esak2000
  • 2
4 Comments
 
LVL 53

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 1000 total points
ID: 39621287
Hi,

pls try

Sub CallMacro()
mystring = "ree.ff@ch.com, ree.fgf@ch.com, ff.hh@dd.com"
MsgBox TestEmails(mystring)
End Sub

Function TestEmails(strText) As String
Dim oRE As Object
Set oRE = CreateObject("VBScript.RegExp")

oRE.Pattern = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
oRE.Global = True

Set matches = oRE.Execute(strText)
For Each Match In matches
    s = s & " Position: " & Match.FirstIndex 'Position of the match
    s = s & " Word: " & Match.Value 'The actual match
    s = s & Chr(10) 'Prints a new line
Next
    TestEmails = s
End Function

Open in new window

EDIT  Changed as function
Regards
0
 
LVL 43

Expert Comment

by:pcelba
ID: 39621307
FoxPro does not include RegExp natively so you'll need to use some external library. Also different RegExp implementation exists and all of them have some specific features. E-mail address pattern has also many RegExp definitions...

So your problem is not FoxPro problem but more (or less) complex task...

Do you have the e-mail pattern already? Do you have your text string sample?

Some RegExp samples are listed e.g. here: http://fox.wikis.com/wc.dll?Wiki~RegExp
http://fox.wikis.com/wc.dll?Fox~SimplifiedStringParsing

http://foxpro.wikia.com/wiki/Regular_expressions  (This web contains a link to Craig's Boyd FLL which is good. The web just runs some unspecified scripts which may cause IE to freeze...)

RegExp e-mail patterns are e.g. here: http://www.regular-expressions.info/email.html

BTW, why RegExp?  You may simplify the task: E.g. count a number of @ characters in the text and then separate the text around this characters and you have possible e-mail. Now you may validate the e-mail. etc.
0
 
LVL 43

Accepted Solution

by:
pcelba earned 1000 total points
ID: 39621573
Hmm... it seems the function from Rgonzo1971 works well even on web pages which is probably the main goal of this question... Am I right?
LOCAL mystring, laAddr[1]
mystring = "ree.ff@ch.com, ree.fgf@ch.com, ff.hh@dd.com"
*mystring = FILETOSTR("Contact.htm")

? ExtractEmails(mystring, @laAddr)
FOR i = 1 TO ALEN(laAddr)
  ? laAddr[i]
NEXT

PROCEDURE ExtractEmails
LPARAMETERS lcText, laOut

LOCAL oRE As VBScript.RegExp, match, matches, lnCount
oRE = CreateObject("VBScript.RegExp")

oRE.Pattern = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
oRE.Global = .T.

lnCount = 0
matches = oRE.Execute(m.lcText)
For Each Match In matches
    lnCount = m.lnCount + 1
    DIMENSION laOut[m.lnCount]
    laOut[m.lnCount] = Match.Value
Next

RETURN m.lnCount

Open in new window

0
 

Author Closing Comment

by:esak2000
ID: 39622470
Hi pcelba,

Yes, it's for web pages. Your code is what I was looking for. Rgonzo1971 code looks good, but seems like it's written in vb.net

Thank you
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Microsoft Visual FoxPro (short VFP) is a programming language with it’s own IDE and database, ranking somewhat between Access and VB.NET + SQL Server (Express). Product Description: http://msdn.microsoft.com/en-us/vfoxpro/default.aspx (http://msd…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question