/ / Regulärer Ausdruck um eine Email in Perl zu validieren

Regulärer Ausdruck um eine Email in Perl zu validieren

Ich bin neu in Perl und versuche, das zu erreichenSkriptsprache, in der ich auf reguläre Ausdrücke stoße, um eine E-Mail-Adresse zu validieren. Ich teile das Perl-Skript. Ich bin mir nicht sicher, wo ich Fehler mache. Der @ -Teil wird immer weggelassen, nach dem die korrekte E-Mail-ID ebenfalls als ungültig angezeigt wird.

Hier ist der Code:

#!/usrs/bin/perl/
$string = "XYZ@yahoo.com";
if ( $string =~ /([a-zA-Z]+)@([a-zA-Z]+).(com|net|org)/)
{
print "TRUE";
print $string;
}
else
{
print "FALSE";
print $string;
}

Danke für Ihre Hilfe.

Antworten:

6 für die Antwort № 1

Die Regex für die Validierung einer E-Mail-Adresse ist in der Quelltext für Email :: Gültig. Ich habe es unten kopiert:

$RFC822PAT = <<"EOF";
[40t]*(?:([^\x80-xffn15()]*(?:(?:\[^x80-xff]|([^\x80-
xffn15()]*(?:\[^x80-xff][^\x80-xffn15()]*)*))[^\x80-xf
fn15()]*)*)[40t]*)*(?:(?:[^(40)<>@,;:".\[]00-37x80-x
ff]+(?![^(40)<>@,;:".\[]00-37x80-xff])|"[^\x80-xffn15
"]*(?:\[^x80-xff][^\x80-xffn15"]*)*")[40t]*(?:([^\x80-
xffn15()]*(?:(?:\[^x80-xff]|([^\x80-xffn15()]*(?:\[^x80
-xff][^\x80-xffn15()]*)*))[^\x80-xffn15()]*)*)[40t]*
)*(?:.[40t]*(?:([^\x80-xffn15()]*(?:(?:\[^x80-xff]|([^
\x80-xffn15()]*(?:\[^x80-xff][^\x80-xffn15()]*)*))[^\
x80-xffn15()]*)*)[40t]*)*(?:[^(40)<>@,;:".\[]00-37x8
0-xff]+(?![^(40)<>@,;:".\[]00-37x80-xff])|"[^\x80-xffn
15"]*(?:\[^x80-xff][^\x80-xffn15"]*)*")[40t]*(?:([^\x
80-xffn15()]*(?:(?:\[^x80-xff]|([^\x80-xffn15()]*(?:\[^
x80-xff][^\x80-xffn15()]*)*))[^\x80-xffn15()]*)*)[40
t]*)*)*@[40t]*(?:([^\x80-xffn15()]*(?:(?:\[^x80-xff]|([
^\x80-xffn15()]*(?:\[^x80-xff][^\x80-xffn15()]*)*))[^
\x80-xffn15()]*)*)[40t]*)*(?:[^(40)<>@,;:".\[]00-37
x80-xff]+(?![^(40)<>@,;:".\[]00-37x80-xff])|[(?:[^\x80-
xffn15[]]|\[^x80-xff])*])[40t]*(?:([^\x80-xffn15()
]*(?:(?:\[^x80-xff]|([^\x80-xffn15()]*(?:\[^x80-xff][^\
x80-xffn15()]*)*))[^\x80-xffn15()]*)*)[40t]*)*(?:.[4
0t]*(?:([^\x80-xffn15()]*(?:(?:\[^x80-xff]|([^\x80-xff
n15()]*(?:\[^x80-xff][^\x80-xffn15()]*)*))[^\x80-xffn
015()]*)*)[40t]*)*(?:[^(40)<>@,;:".\[]00-37x80-xff]+(?!
[^(40)<>@,;:".\[]00-37x80-xff])|[(?:[^\x80-xffn15[
]]|\[^x80-xff])*])[40t]*(?:([^\x80-xffn15()]*(?:(?:\[^
x80-xff]|([^\x80-xffn15()]*(?:\[^x80-xff][^\x80-xffn1
5()]*)*))[^\x80-xffn15()]*)*)[40t]*)*)*|(?:[^(40)<>@,;:".
\[]00-37x80-xff]+(?![^(40)<>@,;:".\[]00-37x80-xff]
)|"[^\x80-xffn15"]*(?:\[^x80-xff][^\x80-xffn15"]*)*")[^
()<>@,;:".\[]x80-xff00-1012-37]*(?:(?:([^\x80-xffn
15()]*(?:(?:\[^x80-xff]|([^\x80-xffn15()]*(?:\[^x80-xff][
^\x80-xffn15()]*)*))[^\x80-xffn15()]*)*)|"[^\x80-xff
n15"]*(?:\[^x80-xff][^\x80-xffn15"]*)*")[^()<>@,;:".\[]
x80-xff00-1012-37]*)*<[40t]*(?:([^\x80-xffn15()]*(?
:(?:\[^x80-xff]|([^\x80-xffn15()]*(?:\[^x80-xff][^\x80-
xffn15()]*)*))[^\x80-xffn15()]*)*)[40t]*)*(?:@[40t]*
(?:([^\x80-xffn15()]*(?:(?:\[^x80-xff]|([^\x80-xffn15
()]*(?:\[^x80-xff][^\x80-xffn15()]*)*))[^\x80-xffn15()
]*)*)[40t]*)*(?:[^(40)<>@,;:".\[]00-37x80-xff]+(?![^(
40)<>@,;:".\[]00-37x80-xff])|[(?:[^\x80-xffn15[]]|\
[^x80-xff])*])[40t]*(?:([^\x80-xffn15()]*(?:(?:\[^x80-
xff]|([^\x80-xffn15()]*(?:\[^x80-xff][^\x80-xffn15()]*
)*))[^\x80-xffn15()]*)*)[40t]*)*(?:.[40t]*(?:([^\x80
-xffn15()]*(?:(?:\[^x80-xff]|([^\x80-xffn15()]*(?:\[^x
80-xff][^\x80-xffn15()]*)*))[^\x80-xffn15()]*)*)[40t
]*)*(?:[^(40)<>@,;:".\[]00-37x80-xff]+(?![^(40)<>@,;:".\
[]00-37x80-xff])|[(?:[^\x80-xffn15[]]|\[^x80-xff])
*])[40t]*(?:([^\x80-xffn15()]*(?:(?:\[^x80-xff]|([^\x
80-xffn15()]*(?:\[^x80-xff][^\x80-xffn15()]*)*))[^\x80
-xffn15()]*)*)[40t]*)*)*(?:,[40t]*(?:([^\x80-xffn15(
)]*(?:(?:\[^x80-xff]|([^\x80-xffn15()]*(?:\[^x80-xff][^\
x80-xffn15()]*)*))[^\x80-xffn15()]*)*)[40t]*)*@[40t
]*(?:([^\x80-xffn15()]*(?:(?:\[^x80-xff]|([^\x80-xffn
15()]*(?:\[^x80-xff][^\x80-xffn15()]*)*))[^\x80-xffn15
()]*)*)[40t]*)*(?:[^(40)<>@,;:".\[]00-37x80-xff]+(?![^(
40)<>@,;:".\[]00-37x80-xff])|[(?:[^\x80-xffn15[]]|
\[^x80-xff])*])[40t]*(?:([^\x80-xffn15()]*(?:(?:\[^x80
-xff]|([^\x80-xffn15()]*(?:\[^x80-xff][^\x80-xffn15()
]*)*))[^\x80-xffn15()]*)*)[40t]*)*(?:.[40t]*(?:([^\x
80-xffn15()]*(?:(?:\[^x80-xff]|([^\x80-xffn15()]*(?:\[^
x80-xff][^\x80-xffn15()]*)*))[^\x80-xffn15()]*)*)[40
t]*)*(?:[^(40)<>@,;:".\[]00-37x80-xff]+(?![^(40)<>@,;:".
\[]00-37x80-xff])|[(?:[^\x80-xffn15[]]|\[^x80-xff
])*])[40t]*(?:([^\x80-xffn15()]*(?:(?:\[^x80-xff]|([^\
x80-xffn15()]*(?:\[^x80-xff][^\x80-xffn15()]*)*))[^\x
80-xffn15()]*)*)[40t]*)*)*)*:[40t]*(?:([^\x80-xffn15
()]*(?:(?:\[^x80-xff]|([^\x80-xffn15()]*(?:\[^x80-xff][^
\x80-xffn15()]*)*))[^\x80-xffn15()]*)*)[40t]*)*)?(?:[^
(40)<>@,;:".\[]00-37x80-xff]+(?![^(40)<>@,;:".\[]00-
37x80-xff])|"[^\x80-xffn15"]*(?:\[^x80-xff][^\x80-xff
n15"]*)*")[40t]*(?:([^\x80-xffn15()]*(?:(?:\[^x80-xff]|
([^\x80-xffn15()]*(?:\[^x80-xff][^\x80-xffn15()]*)*))
[^\x80-xffn15()]*)*)[40t]*)*(?:.[40t]*(?:([^\x80-xff
n15()]*(?:(?:\[^x80-xff]|([^\x80-xffn15()]*(?:\[^x80-x
ff][^\x80-xffn15()]*)*))[^\x80-xffn15()]*)*)[40t]*)*(
?:[^(40)<>@,;:".\[]00-37x80-xff]+(?![^(40)<>@,;:".\[]
000-37x80-xff])|"[^\x80-xffn15"]*(?:\[^x80-xff][^\x80-
xffn15"]*)*")[40t]*(?:([^\x80-xffn15()]*(?:(?:\[^x80-x
ff]|([^\x80-xffn15()]*(?:\[^x80-xff][^\x80-xffn15()]*)
*))[^\x80-xffn15()]*)*)[40t]*)*)*@[40t]*(?:([^\x80-x
ffn15()]*(?:(?:\[^x80-xff]|([^\x80-xffn15()]*(?:\[^x80-
xff][^\x80-xffn15()]*)*))[^\x80-xffn15()]*)*)[40t]*)
*(?:[^(40)<>@,;:".\[]00-37x80-xff]+(?![^(40)<>@,;:".\[
]00-37x80-xff])|[(?:[^\x80-xffn15[]]|\[^x80-xff])*]
)[40t]*(?:([^\x80-xffn15()]*(?:(?:\[^x80-xff]|([^\x80-
xffn15()]*(?:\[^x80-xff][^\x80-xffn15()]*)*))[^\x80-x
ffn15()]*)*)[40t]*)*(?:.[40t]*(?:([^\x80-xffn15()]*(
?:(?:\[^x80-xff]|([^\x80-xffn15()]*(?:\[^x80-xff][^\x80
-xffn15()]*)*))[^\x80-xffn15()]*)*)[40t]*)*(?:[^(40)<
>@,;:".\[]00-37x80-xff]+(?![^(40)<>@,;:".\[]00-37x8
0-xff])|[(?:[^\x80-xffn15[]]|\[^x80-xff])*])[40t]*(?:
([^\x80-xffn15()]*(?:(?:\[^x80-xff]|([^\x80-xffn15()]
*(?:\[^x80-xff][^\x80-xffn15()]*)*))[^\x80-xffn15()]*)
*)[40t]*)*)*>)
EOF

Aber dein tatsächliches Problem ist diese Zeile:

$string = "XYZ@yahoo.com";

Das @yahoo sieht aus wie eine Array-Variable für Perl, und Sie haben kein Array namens @yahoo Es wird durch eine leere Zeichenfolge ersetzt. Versuchen Sie, den Wert von zu drucken $string um zu sehen, was du bekommst.

Die Lösung besteht darin, einfache Anführungszeichen zu verwenden (die Array-Variable wird also nicht erweitert):

$string = "XYZ@yahoo.com";

Oder um dem zu entkommen @ mit einem :

$string = "XYZ@yahoo.com";

Immer hinzufügen use strict und use warnings zu Ihren Perl-Programmen. Sie hätten dir gesagt, was das Problem ist.


6 für die Antwort № 2
#!/usr/bin/perl
use strict;
use warnings;

use Email::Valid;

my $email_address = "a.n@example.com";

unless( Email::Valid->address($email_address) ) {
print "Sorry, that email address is not valid!";
}

Referenzseite: http://learn.perl.org/examples/email_valid.html

Für Regex Pattern versuche dies:

 my $pattern= "^([a-zA-Z][w_.]{6,15})@([a-zA-Z0-9.-]+).([a-zA-Z]{2,4})$";

Referenzseite: https://www.experts-exchange.com/articles/8652/Email-validation-using-Regular-Expression-in-Perl.html


0 für die Antwort № 3

Ändern Sie Ihre if-Bedingung in

  if ($string =~ /^[a-z0-9A-Z][A-Za-z0-9.]+[A-Za-z0-9]@[A-Za-z0-9.-]+$/)

und ändern $string = "XYZ@yahoo.com"; zu

$string = "XYZ@yahoo.com";

Reference: http://perlmaven.com/email-validation-using-regular-expression-in-perl für Details.


0 für die Antwort № 4

Versuchen

if ($email =~ /^[a-z0-9]([a-z0-9.]+[a-z0-9])?@[a-z0-9.-]+$/)

Und ein paar Test-Code:

#!/usr/bin/perl

my $email = "john.doe@acme.org";

if ($email =~ /^[a-z0-9]([a-z0-9.]+[a-z0-9])?@[a-z0-9.-]+$/) {
print "Valid emailn";
} else {
print "Not valid emailn";
}

exit;

Ausgabe:

Valid email