Extract email from string

Extract email from string

Postby Marc Venken » 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.04 with Harbour
User avatar
Marc Venken
 
Posts: 1431
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: Extract email from string

Postby cnavarro » Wed Jun 28, 2017 1:17 am

I do not know if I understand correctly what you are asking for
But, try with

Code: Select all  Expand view


   local cMail  := "slk slkjsd ljd d mycta.withmore@gmail.com 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
User avatar
cnavarro
 
Posts: 6541
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: Extract email from string

Postby Marc Venken » 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.04 with Harbour
User avatar
Marc Venken
 
Posts: 1431
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: Extract email from string

Postby bpd2000 » Wed Jun 28, 2017 1:22 pm

Try:
Code: Select all  Expand view

   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.!
User avatar
bpd2000
 
Posts: 153
Joined: Tue Aug 05, 2014 9:48 am
Location: India

Re: Extract email from string

Postby cnavarro » Wed Jun 28, 2017 1: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
User avatar
cnavarro
 
Posts: 6541
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: Extract email from string

Postby Marc Venken » Wed Jun 28, 2017 2: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: Select all  Expand view
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 info@maveco.be"

   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.04 with Harbour
User avatar
Marc Venken
 
Posts: 1431
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: Extract email from string

Postby karinha » Wed Jun 28, 2017 5:44 pm

Use,

Code: Select all  Expand view

   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
User avatar
karinha
 
Posts: 7813
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 100 guests