Solved

Foxpro - finding all email addresses in string with Regex

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

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 42

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 42

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VFP9 in Windows 10 9 350
Replace a tag with sed 2 52
How to replace a token in a string with square brackets? 2 77
Regular Expression Calculator Tester 2 81
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

829 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