FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Extract email from string
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Extract email from string
Posted: Tue Jun 27, 2017 11:27 PM

hello,

Someone has a function to get emails out of a string

I read a website and want to pull the emails out of it. The cTxt (content) can have more than one email.

Is there a function that reads back from a string ?

The At(....) will give the position of the @ sign, but i have to go to the front for the first blanco char, and also go to the back to finaly have
ex. mymail@mydomain.com

Marc Venken

Using: FWH 23.08 with Harbour
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: Extract email from string
Posted: Wed Jun 28, 2017 01:17 AM
I do not know if I understand correctly what you are asking for
But, try with

Code (fw): Select all Collapse
   local cMail  := "slk slkjsd ljd d <!-- e --><a href="mailto:mycta.withmore@gmail.com">mycta.withmore@gmail.com</a><!-- e --> fnsjhkjsh sdhksjdhk"
   local c1     := Left( cMail, At( "@", cMail ) )
   local c2     := Right( cMail, Len( cMail ) - At( "@", cMail ) )
   local c      := AllTrim( Substr( cMail, Rat( " ", c1 ), At( " ", c2 ) + ( Len( c1 ) - Rat( " ", c1 ) ) ) )
   ? c
Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo

El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: Extract email from string
Posted: Wed Jun 28, 2017 10:28 AM

Yes this works if the leading and trailing char = space,
But I have the str !!, so I have to fine-tune the result more.

I also get this as result :

href="mailto:info@dbm-essen.be">info@dbm-essen.be</a>

I will have to work with some if/then to finaly get to the email itself.
If mailto: is in the str, then return a substr(to difine)

I think that there will be other str's to come with more options.

Marc Venken

Using: FWH 23.08 with Harbour
Posts: 153
Joined: Tue Aug 05, 2014 09:48 AM
Re: Extract email from string
Posted: Wed Jun 28, 2017 01:22 PM
Try:
Code (fw): Select all Collapse
   PROCEDURE Main
      LOCAL cRegEx := "[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}"
      LOCAL cText  := "Send your request to 'mailto:info@dbm-essen.be>info@dbm-essen.be '" + ;
                      "for more information"
      LOCAL cEmail, nStart, nLen

     cEmail := HB_AtX( cRegEx, cText, .F., @nStart, @nLen )
     
     ? cEmail
     inkey(0)
   RETURN
Regards, Greetings



Try FWH. You will enjoy it's simplicity and power.!
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: Extract email from string
Posted: Wed Jun 28, 2017 01:28 PM

Great, very good

Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo

El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: Extract email from string
Posted: Wed Jun 28, 2017 02:28 PM
Indeed, very good and with one line, Top !!

I wanted to go one step further, but this does not work, because I don't untherstand
what the function is doing ? Could you explane the process ?

What I tried : Get all the emails out of the string

Code (fw): Select all Collapse
function extractonline()
   local aMails:={}
   LOCAL cRegEx := "[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}"
   LOCAL cEmail, nStart, nLen
   Local cText  := "Send your request to 'mailto:info@dbm-essen.be>info@dbm-essen.be '" + ;
                      "for more information or to 'mailto:info@dbm-essen.be>info@dbm-essen.be ' and finaly to <!-- e --><a href="mailto:info@maveco.be">info@maveco.be</a><!-- e -->"

   do while at("@",cText) > 0
       ? cText
       cEmail := HB_AtX( cRegEx, cText, .F., @nStart, @nLen )
       ? cEmail
       AADD(aMails,cEmail)
       cText = substr(cText,at("@",cText)+1)
   enddo
   xbrowser(aMails)
return



Perl-5 compatible RegEx:
------------------------

xHarbour includes PCRE which is a full feature, Perl 5 compatible, Regular
Expression engine. Full feature Search & Replace classes are currently under
construction, but you may already use the full power of RegEx searches,
new operators, HAS and LIKE.

cExp HAS cPatern|RegEx => bFound
cExp LIKE cPatern|RegEx => bLike

As well as full featured Functions:

HB_Atx( <cRegEx>, <cTargetString> [, lCaseSensitive [, [@]nStart ]
[, [@]nLen ] ] ] ) => cFoundText
Marc Venken

Using: FWH 23.08 with Harbour
Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: Extract email from string
Posted: Wed Jun 28, 2017 05:44 PM
Use,

Code (fw): Select all Collapse
   cEmail     := ALLTRIM( cEmailCliente )

    cEmailErro := HB_aTokens( cEmail, ";" )


http://forums.fivetechsupport.com/viewtopic.php?f=6&t=34245

Regards, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341

Continue the discussion