Re: E-mail pattern Was: Help on regex script..
"valery_vi" <[email protected]>
| Newsgroups | gmane.comp.windows.autoit.user |
|---|---|
| Message-ID | <[email protected]> |
Florent,
This script (more short!) works for me:
$file = FileOpen("test.csv", 0)
; Check if file opened for reading OK
If $file = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
$line_number = 0
$codetiersPattern = '[1]{1}[0-9]{8}'
$telephonePattern = '[0]{1}[12345689]{1}[0-9]{8}'
$gsmPattern = '[0]{1}[6]{1}[0-9]{8}'
$emailPattern = '[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z](?i)'
;Read in lines of text until the EOF is reached
While 1
$line_number = $line_number+1
;Get file current line content
$line = FileReadLine($file)
If @error = -1 Then ExitLoop
MsgBox(0, "Line read :", $line)
If $line_number == 1 Then
MsgBox(0,"Information","Je lis la premiere ligne")
Else
$Columns = StringSplit($line, Chr(9), 1)
; test CODE_TIERS
if IsNotValidField($Columns[1], $codetiersPattern, $line_number, 1)
then exit
; test TELEPHONE
if IsNotValidField($Columns[2], $telephonePattern, $line_number, 2)
then exit
; test GSM
if IsNotValidField($Columns[3], $gsmPattern, $line_number, 3) then
exit
; test email
if IsNotValidField($Columns[4], $emailPattern, $line_number, 4)
then exit
EndIf
WEnd
FileClose($file)
exit
; validation function
func IsNotValidField($Target, $Pattern, $Row, $Col)
local $res
$res = StringRegExp($Target, $Pattern, 1)
if Not @Error then return 0
MsgBox(0, 'Error!', 'Row: ' & $Row & ' Col: ' & $Col & @crlf &
$Target)
return 1
endfunc
with data file like this:
CODE_TIERS TELEPHONE GSM EMAIL
100000002 0955480155 0660485560 [email protected]
1000000042 0955480101 091242847 himothercompany.com
Note: Field delimiters is Tab characters!
Valery
--- In [email protected], "Florent Gilain"
<florent.gilain@...> wrote:
>
> Hello Valery,
>
> Sorry, i'm still unable to make it work..
>
> here si my new autoit code :
>
> $file = FileOpen("test.csv", 0)
>
> ; Check if file opened for reading OK
> If $file = -1 Then
>
> MsgBox(0, "Error", "Unable to open file.")
> Exit
> EndIf
>
> $line_number = 0
>
> $codetiersPattern = '[1]{1}[0-9]{8}'
> $telephonePattern = '[0]{1}[12345689]{1}[0-9]{8}'
> $gsmPattern = '[0]{1}[6]{1}[0-9]{8}'
> $emailPattern = '[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z](?i)'
>
> ; Read in lines of text until the EOF is reached
> While 1
> ; On incrémente le compteur du nombre de lignes
> $line_number = $line_number+1
> ; On récupère le contenu de la ligne en cours / Get file current
line
> content
> $line = FileReadLine($file)
> If @error = -1 Then ExitLoop
>
> MsgBox(0, "Line read :", $line)
> ; On évite la premiere ligne qui contient les en-têtes / don't act
on first
> line because it only contains headers of the columns
> If $line_number == 1 Then
> MsgBox(0,"Information","Je lis la premiere ligne")
> Else
> ; On récupère chaque champ dans un tableau de variable en
utilisant la
> tabulation comme séparateur / Get each column value (cvs file with
> tabulation separator) in an array
> $array = StringSplit($line, Chr(9), 1)
>
> $RegExResult = StringRegExp($array[1], $codetiersPattern, 1)
> If $RegExResult <> 0 Then
> MsgBox(0, "Line number " & $line_number & " / Column 1 : KO",
> $array[1])
> EndIf
>
> $RegExResult = StringRegExp($array[2], $telephonePattern, 1)
> If $RegExResult <> 0 Then
> MsgBox(0, "Line number " & $line_number & " / Column 2 : KO",
> $array[2])
> EndIf
>
> $RegExResult = StringRegExp($array[3], $gsmPattern, 1)
> If $RegExResult <> 0 Then
> MsgBox(0, "Line number " & $line_number & " / Column 3 : KO",
> $array[3])
> EndIf
>
> $RegExResult = StringRegExp($array[4], $emailPattern, 1)
> If $RegExResult <> 0 Then
> MsgBox(0, "Line number " & $line_number & " / Column 4 : KO",
> $array[4])
> EndIf
> EndIf
> WEnd
>
> FileClose($file)
>
> any idea of what i missed ? i get a KO result (not OK) even if it
should be
> ok
>
> florent
>
> _____
>
> De : [email protected] [mailto:[email protected]]
De la
> part de valery_vi
> Envoyé : mardi 30 janvier 2007 07:26
> À : [email protected]
> Objet : [AutoIt] E-mail pattern Was: Help on regex script..
>
>
>
> But it isn't perfect and is a subject of many articles
> See, for example:
> http://www.regular- <http://www.regular-expressions.info/email.html>
> expressions.info/email.html
>
> Valery
>
> --- In AutoItList@yahoogro <mailto:AutoItList%40yahoogroups.com>
ups.com,
> "valery_vi" <shehrer1@> wrote:
> >
> > About e-mail. I found this nice pattern
> >
> > $Pattern = '[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z](?i)'
> >
> > $RegExResult = StringRegExp($array[1], $Pattern, 1)
> >
> > It works with ARegExp script, too.
> >
> > Valery
> >
> >
> > --- In AutoItList@yahoogro <mailto:AutoItList%40yahoogroups.com>
ups.com,
> "valery_vi" <shehrer1@> wrote:
> > >
> > > Florent,
> > >
> > > I'm not expert in regexp but maybe it will be helpful to use
> regexp
> > > without grouping parenthethis '(...)'
> > >
> > > > Column 1 : 9 numbers with first number = 1 (example :
> 100000058)
> > > $RegExResult = StringRegExp($array[1], '[1][0-9]{8}', 1)
> > > > Column 2 : 10 numbers with first number = 0 (example :
> 060254589)
> > > > Column 3 : 10 numbers with first number = 0 (example :
> 060254589)
> > > $RegExResult = StringRegExp($array[1], '[0][0-9]{9}', 1)
> > >
> > > > Column 4 : valid email address
> > > It is expert's case.
> > > ;-(
> > >
> > >
> > > Valery
> > >
> > > --- In AutoItList@yahoogro <mailto:AutoItList%40yahoogroups.com>
> ups.com, "Florent Gilain"
> > > <florent.gilain@> wrote:
> > > >
> > > > Hi all,
> > > >
> > > > I have to create a script that will load a file containing
> lines
> > > separated
> > > > by tabulation.
> > > > Then, i need to verify data format using Regular expression;
> but
> > > i'm unable
> > > > to understand how it works in AutoIT...i have read the help
> file
> > and
> > > > tutorial, but it seems i missed something...
> > > >
> > > > File would contain :
> > > >
> > > > Column 1 : 9 numbers with first number = 1 (example :
> 100000058)
> > > > Column 2 : 10 numbers with first number = 0 (example :
> 060254589)
> > > > Column 3 : 10 numbers with first number = 0 (example :
> 060254589)
> > > > Column 4 : valid email address
> > > >
> > > > Example of file containing data :
> > > >
> > > > CODE_TIERS TELEPHONE GSM EMAIL
> > > > 100000002 0955480155 0906604855 me@ (valid line)
> > > > 1000000042 0955480101 091242847
> himothercompany.com
> > > (invalid
> > > > line)
> > > >
> > > >
> > > > Here is what i have tryed :
> > > >
> > > > $file = FileOpen("test.csv", 0)
> > > >
> > > > ; Check if file opened for reading OK
> > > > If $file = -1 Then
> > > >
> > > > MsgBox(0, "Error", "Unable to open file.")
> > > > Exit
> > > > EndIf
> > > >
> > > > $line_number = 0
> > > >
> > > > ; Read in lines of text until the EOF is reached
> > > > While 1
> > > > $line = FileReadLine($file)
> > > > If @error = -1 Then ExitLoop
> > > > $line_number = $line_number+1
> > > > MsgBox(0, "Line read :", $line)
> > > >
> > > > $array = StringSplit($line, Chr(9), 1)
> > > > MsgBox(0, "Line number " & $line_number & " / Column 1 :",
> > > > $array[1])
> > > > $RegExResult = StringRegExp($array[1], '([0]{1}[0-9]{8})', 1)
> > > > If @error == 0 Then
> > > > MsgBox(0, "Line number " & $line_number & " / Column 1 :
> KO",
> > > $array[1])
> > > > EndIf
> > > >
> > > > MsgBox(0, "Line number " & $line_number & " / Column 2 :",
> > > > $array[2])
> > > > $RegExResult = StringRegExp($array[2], '([0]{1}[0-9]{9})', 1)
> > > > If @error == 0 Then
> > > > MsgBox(0, "Line number " & $line_number & " / Column 2 :
> KO",
> > > $array[2])
> > > > EndIf
> > > >
> > > > MsgBox(0, "Line number " & $line_number & " / Column 3 :",
> > > > $array[3])
> > > > $RegExResult = StringRegExp($array[3], '([0]{1}[0-9]{9})', 1)
> > > > If @error == 0 Then
> > > > MsgBox(0, "Line number " & $line_number & " / Column 3 :
> KO",
> > > $array[3])
> > > > EndIf
> > > >
> > > > MsgBox(0, "Line number " & $line_number & " / Column 4 :",
> > > > $array[4])
> > > > $RegExResult = StringRegExp($array[4],
> > > > '(^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-
]?
> [a-
> > > zA-Z0-9]+)
> > > > *)\.([A-Za-z]{2,})$)', 1)
> > > > If @error == 0 Then
> > > > MsgBox(0, "Line number " & $line_number & " / Column 4 :
> KO",
> > > $array[4])
> > > > EndIf
> > > >
> > > > Wend
> > > >
> > > > FileClose($file)
> > > >
> > > > Any idea of what i missed ?
> > > >
> > > > Thanks
> > > >
> > > > Florent
> > > >
> > >
> >
>
>
>
>
>
>
> [Non-text portions of this message have been removed]
>