Solved

Foxpro - finding all email addresses in string with Regex

Posted on 2013-11-04
4
1,339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…
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…

756 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