Solved

Foxpro - finding all email addresses in string with Regex

Posted on 2013-11-04
4
1,270 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 48

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 250 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 41

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 41

Accepted Solution

by:
pcelba earned 250 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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,…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now