search_emailSyntaxeSearchEmail(texte) Extrait les emails d'une chaîne de caractères sous forme de liste. Le pramètre et le résultat sont au format texte. ExempleSearchEmail("wanted 3000 $ averel@dalton.org and joe@dalton.org dead or alive") --->{"averel@dalton.org", "joe@dalton.org"} Scripton SearchEmail(theString) set listEmail to "" set the theString to " " & theString & " " repeat until the length of theString is 0 set Arob to offset of "@" in theString if Arob = 0 then exit repeat set debutToArob to (characters Arob thru 1 of theString as string) set ArobTodebut to ((reverse of (every character of debutToArob as list)) as string) set espace to offset of " " in ArobTodebut set debutMail to Arob - espace + 2 set fin to the length of theString set temp to characters debutMail thru fin of theString as string set finmail to (offset of " " in temp) - 2 set mail to (characters debutMail thru (debutMail + finmail)) of theString as string set listEmail to (listEmail & mail as list) set theString to characters (debutMail + finmail + 1) thru fin of theString as string end repeat return listEmail end SearchEmail
RemarqueLe principe consiste à utiliser offset pour rechercher l'arobase puis à partir de là, dans un 1er temps, remonter la chaïne jusqu'à un " " et dans un 2ème temps la descendre jusqu'à un espace pour avoir l'email au complet. Reverse permet de pouvoir utiliser offset dans le 2ème temps descendant qui devient donc ascendant (relire plusieurs fois ;-) L'ajout d'une espace " " en début et en fin de chaîne (set the theString to " " & theString & " "), évite que la commande offset ne renvoie un message d'erreur à la fin de l'éxécution de la routine.
|