• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1570
  • Last Modified:

Foxpro - finding all email addresses in string with Regex

How do I find multiple email addresses in a string using Foxpro and Regex?
0
esak2000
Asked:
esak2000
  • 2
2 Solutions
 
Rgonzo1971Commented:
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
 
pcelbaCommented:
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
 
pcelbaCommented:
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
 
esak2000Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now