Jump to content
Sign in to follow this  

StringRegExpReplace from text

Recommended Posts

Hello ..

hello guys!
I need your help a few minutes!

I have a maney text file, how could an algorithm, to extract only the email addresses those that contain "." and "@"

If you can help me, I'll be very grateful, thank you!

Share this post

Link to post
Share on other sites

Take a look to this page, you will find several regular expresions (explained) to match email addresses. One you get the right one, you can use it on autoit using StringRegExp or StringRegExpReplace functions.

Share this post

Link to post
Share on other sites

Not working for me :sweating:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StringConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 251, 192, 124)
$Edit1 = GUICtrlCreateEdit("", 16, 8, 593, 169, $ES_WANTRETURN)
GUICtrlSetData(-1, StringFormat("As I explain below, my claim only holds true testmail@mail.com when one accepts my definition of what a valid email \r\naddress really is, and what \r\nit" & Chr(39) & "s not. If you want to use a different definition, you" & Chr(39) & "ll have to adapt the mail2@gmail.com regex. Matching a valid email \r\naddress is a perfect \r\nexample showing that before writing m3@ts1.es a regex, you have to know exactly what you" & Chr(39) & "re trying to match, and what \r\nnot; and \r\nthere" & Chr(39) & "s often a trade-off between what" & Chr(39) & "s exact, and what" & Chr(39) & "s practical."))
$Button1 = GUICtrlCreateButton("Show emails", 16, 192, 419, 25)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
        Case $Button1
            $aArray = StringRegExp(GUICtrlRead($Edit1), '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b', $STR_REGEXPARRAYMATCH)
            For $i = 0 To UBound($aArray) - 1
                MsgBox(0, "" & $i, $aArray[$i])


probably am wrong somewhere....

Share this post

Link to post
Share on other sites

In the sets you use A-Z which matches uppercase while the mail addresses are lowercase, so you must make the regex case insensitive using (?i)

BTW better use  $STR_REGEXPARRAYGLOBALMATCH  to get all the matches instead of  $STR_REGEXPARRAYMATCH  which returns only the first one

Edited by mikell

Share this post

Link to post
Share on other sites

Not working for me :sweating:


mikell pointed to the the solution to solve your problem. You have two options, make the search case insensitive

$aArray = StringRegExp(GUICtrlRead($Edit1), '(?i)\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b',  $STR_REGEXPARRAYGLOBALMATCH)

or add a-z to the pattern to match lowercase letters.

$aArray = StringRegExp(GUICtrlRead($Edit1), '\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b',  $STR_REGEXPARRAYGLOBALMATCH)

Anyway, in the page I linked yesterday, you had this info ;) "If you want to use the regular expression above, there's two things you need to understand. First, long regexes make it difficult to nicely format paragraphs. So I didn't include a-z in any of the three character classes. This regex is intended to be used with your regex engine's "case insensitive" option turned on."



Edited by sahsanu

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Create New...