473,383 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

help with regular expression interpretation


function testemail($email) {

$validEmailExpr =
"^[0-9a-z~!#$%&_-]([.]?[0-9a-z~!#$%&_-])*@[0-9a-z~!#$%&_-]([.]?[0-9a-z~!#$%&_-])*$";

return eregi($validEmailExpr, $email);
}

$email = "fo*@bar.gov.mil";
testmail($email); //return TRUE

$email = "fo*****@bar.gov.mil";
testmail($email); //return TRUE

$email = "foo..ba*@bar.gov.mil";
testmail($email); //return FALSE - why ??

$email = "foo.@bar.gov.mil";
testmail($email); //return FALSE - why ??

as i understand it : (steps)

1. accept only 1 char of group [0-9a-z~!#$%&_-]
2. require 0 or more chars of group ([.]?[0-9a-z~!#$%&_-])*
-- why foo..bar is not valid input ?,
-- shouldn't it be ([.]?[0-9a-z~!#$%&_-]*)* ?

thank you for your help.

p.s Sorry if my previous post "can i get the public key of client
machine using php" didn't fit right into USENET, i am very very new here
so go easy on me :)
Jul 17 '05 #1
12 1890
Dnia Sun, 01 Aug 2004 21:03:44 +0000, hq4ever (at) 012 (dot) net (dot) il
spłodził(a):
function testemail($email) {

$validEmailExpr =
"^[0-9a-z~!#$%&_-]([.]?[0-9a-z~!#$%&_-])*@[0-9a-z~!#$%&_-]([.]?[0-9a-z~!#$%&_-])*$";

return eregi($validEmailExpr, $email);
}

$email = "fo*@bar.gov.mil";
testmail($email); //return TRUE

$email = "fo*****@bar.gov.mil";
testmail($email); //return TRUE

$email = "foo..ba*@bar.gov.mil";
testmail($email); //return FALSE - why ?? "]([.]?[0-9"

zero or one '.'
$email = "foo.@bar.gov.mil";
testmail($email); //return FALSE - why ??


last char should be one of [0-9a-z~!#$%&_-]..

Try to use:

eregi(""^[a-z0-9_]+@([a-z0-9_]+.)+[a-z]{2,}$", $email);
--
zYm3N[@interia.pl]

..:: C++ | C | PHP | HTML | Delphi | Pascal
..:: >> http://zymen.cjb.net <<
..:: http://zymen.cjb.net/cytowanie.html
Jul 17 '05 #2
On Sun, 01 Aug 2004 20:49:48 +0200, zYm3N wrote:
[ re: e-mail syntax validation ]

Try to use:

eregi(""^[a-z0-9_]+@([a-z0-9_]+.)+[a-z]{2,}$", $email);

fo*****@my-domain.com
would fall over drunk in your example.

Regards,

Ian
PS: _ (underscore) is not a valid char within a domain name.

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #3
for what input will this "^([.]?[0-9a-z~!#$%&_-])*$" regular expression
be TRUE ?

as i understand, it should accept at most one time '.' & then one
character, no? and so it can be repeated n times, something like
..f.o.o.b.a.r which should evaluate as TRUE, why is it then that a string
such as .my-domain.root still evaluates TRUE ?
Jul 17 '05 #4
On Mon, 02 Aug 2004 02:37:40 +0000, maxim vexler <hq4ever (at) 012 (dot)
net (dot) il> wrote:
for what input will this "^([.]?[0-9a-z~!#$%&_-])*$" regular expression
be TRUE ?

as i understand, it should accept at most one time '.' & then one
character, no? and so it can be repeated n times, something like
.f.o.o.b.a.r which should evaluate as TRUE, why is it then that a string
such as .my-domain.root still evaluates TRUE ?

Maxim,

Although not 100% fail-safe (nothing will be with checks such as this), I
use this function I wrote normally:
function validate_email($addy, $return_mx_records = false) {
if (empty($addy)) return false;

if (!preg_match(
'/^[a-zA-Z0-9&\'\.\-_\+]+\@[a-zA-Z0-9.-]+\.+[a-zA-Z]{2,6}$/',
$addy
)) {
return false;
}

$mx_exists = false;
$mx_records = array();
if (getmxrr(array_pop(explode('@', $addy)), $mx_records)) {
$mx_exists = true;
}

if ($mx_exists) {
return ($return_mx_records) ? $mx_records : true;
} else {
unset($mx_records);
return false;
}
}

USAGE:

if (!validate_email($_POST['email'])) {
$email_valid = false;
/* Do fail stuff */
}

HTH =)

Regards,

Ian
PS: Watch for line wrapping.

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #5
On Mon, 02 Aug 2004 02:47:31 +0000, Ian.H wrote:
if (!preg_match(
'/^[a-zA-Z0-9&\'\.\-_\+]+\@[a-zA-Z0-9.-]+\.+[a-zA-Z]{2,6}$/',
$addy
)) {
return false;
}

Oops, to fix the very issue you mentioned, change the regex to:
'/^[a-zA-Z0-9&\'\.\-_\+]+\@[^\.][a-zA-Z0-9.-]+\.+[a-zA-Z]{2,6}$/'

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #6
In article <pa****************************@bubbleboy.digiserv .net>, Ian.H wrote:
On Mon, 02 Aug 2004 02:47:31 +0000, Ian.H wrote:
if (!preg_match(
'/^[a-zA-Z0-9&\'\.\-_\+]+\@[a-zA-Z0-9.-]+\.+[a-zA-Z]{2,6}$/',
$addy
)) {
return false;
}

Oops, to fix the very issue you mentioned, change the regex to:
'/^[a-zA-Z0-9&\'\.\-_\+]+\@[^\.][a-zA-Z0-9.-]+\.+[a-zA-Z]{2,6}$/'

If it doesn't allow _all_ valid e-mail addresses it's useless.

A quick search in this newsgroup will direct you to phpclasses or
http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html

--
Tim Van Wassenhove <http://home.mysth.be/~timvw>
Jul 17 '05 #7
you have sure given me an answer about "Howto e-mail validation" for
what i thank you.

still, could you explain why the expression in my question didn't
returned what i expected it to return, just for my own knowledge :
for what input will this "^([.]?[0-9a-z~!#$%&_-])*$" regular expression
be TRUE ?

as i understand, it should accept at most one time '.' & then one
character, no? and so it can be repeated n times, something like
.f.o.o.b.a.r which should evaluate as TRUE, why is it then that a string
such as .my-domain.root still evaluates TRUE ?
thank you.

Tim Van Wassenhove wrote: In article <pa****************************@bubbleboy.digiserv .net>, Ian.H wrote:
On Mon, 02 Aug 2004 02:47:31 +0000, Ian.H wrote:

if (!preg_match(
'/^[a-zA-Z0-9&\'\.\-_\+]+\@[a-zA-Z0-9.-]+\.+[a-zA-Z]{2,6}$/',
$addy
)) {
return false;
}

Oops, to fix the very issue you mentioned, change the regex to:
'/^[a-zA-Z0-9&\'\.\-_\+]+\@[^\.][a-zA-Z0-9.-]+\.+[a-zA-Z]{2,6}$/'


If it doesn't allow _all_ valid e-mail addresses it's useless.

A quick search in this newsgroup will direct you to phpclasses or
http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html

Jul 17 '05 #8
On Mon, 02 Aug 2004 12:08:53 +0000, maxim vexler <hq4ever (at) 012 (dot)
net (dot) il> wrote:
still, could you explain why the expression in my question didn't
returned what i expected it to return, just for my own knowledge :

> for what input will this "^([.]?[0-9a-z~!#$%&_-])*$" regular expression
> be TRUE ?

This would allow all your chosen chars above (assuming some of those chars
didn't require escaping, can't think off the top of my head) but I believe
your biggest issue with the above (actually 2):
It tries to validate _starting_ with a . as the first char (for no . at
the beginning, you'd need [^\.] rather than [.] (the ^ in this case,
negates things so "if not ." but don't get that confused with the first ^
(as you have it as that's the start anchor)).

The other issue by the looks of it, '(...)*' the * here specifying zero or
more occurrances, so 'foo@.com' would slide through too.

Something else that you might find useful:
<http://weitz.de/regex-coach/>
I use it normally for more complex regexs, but it's great also for
learning regex too IMO.
HTH =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #9
.oO(maxim vexler <hq4ever (at) 012 (dot) net (dot) il>)
for what input will this "^([.]?[0-9a-z~!#$%&_-])*$" regular expression
be TRUE ?
foo
.foo
foo.bar

but not

foo.
..foo
foo..bar

It accepts every string with chars of the class [0-9a-z~!#$%&_-], if
there's a dot it has to be followed by at least one other char. There
can't be a dot at the end or directly followed by another dot.
as i understand, it should accept at most one time '.' & then one
character, no? and so it can be repeated n times, something like
.f.o.o.b.a.r which should evaluate as TRUE, why is it then that a string
such as .my-domain.root still evaluates TRUE ?


The dot is optional:

[.]?

means zero or one chars of the class [.],

\.?

would do the same in this case.

So the pattern could be read like this: Any number (zero or more) of
chars of the given class, where each char _may_ be preceded by one dot.

BTW: You should use the preg* functions (PCRE) instead of the old ereg*
functions, they're faster and much more flexible.

HTH
Micha
Jul 17 '05 #10
what a wonderful tool, thank you very much.

Ian.H wrote:
Something else that you might find useful:
<http://weitz.de/regex-coach/>
I use it normally for more complex regexs, but it's great also for
learning regex too IMO.
HTH =)

Regards,

Ian

Jul 17 '05 #11
now i see, i was wrong in the way i thought regular expression was
evaluating the text. thank you for your comments.
why do you say preg* is better then ereg*, i've seen the same tip on
php.net but it didn't said any thing about why is it better ? - i know
there are different forms of regular expression (POSIX & Perl no?)
- is it the case with this functions?
- will the syntax be different ?
- do they evaluate differently ?

thank you, you have been great help to me.

Michael Fesser wrote:
.oO(maxim vexler <hq4ever (at) 012 (dot) net (dot) il>)

for what input will this "^([.]?[0-9a-z~!#$%&_-])*$" regular expression
be TRUE ?

foo
.foo
foo.bar

but not

foo.
..foo
foo..bar

It accepts every string with chars of the class [0-9a-z~!#$%&_-], if
there's a dot it has to be followed by at least one other char. There
can't be a dot at the end or directly followed by another dot.

as i understand, it should accept at most one time '.' & then one
character, no? and so it can be repeated n times, something like
.f.o.o.b.a.r which should evaluate as TRUE, why is it then that a string
such as .my-domain.root still evaluates TRUE ?

The dot is optional:

[.]?

means zero or one chars of the class [.],

\.?

would do the same in this case.

So the pattern could be read like this: Any number (zero or more) of
chars of the given class, where each char _may_ be preceded by one dot.

BTW: You should use the preg* functions (PCRE) instead of the old ereg*
functions, they're faster and much more flexible.

HTH
Micha

Jul 17 '05 #12
.oO(maxim vexler <hq4ever (at) 012 (dot) net (dot) il>)
why do you say preg* is better then ereg*, i've seen the same tip on
php.net but it didn't said any thing about why is it better ?
I haven't done a benchmark, but according to the manual they are faster
in most cases. Additionally you can do much more things with them, there
are many very useful extensions and features that are not possible with
the ereg* functions, for example different modifiers to fine-control the
pattern matching, named and conditional subpatterns, assertions ...

This allows much more flexible and sometimes more reliable pattern
matching.
- i know
there are different forms of regular expression (POSIX & Perl no?)
- is it the case with this functions?
ereg* is POSIX, preg* is PCRE (Perl Compatible Regular Expressions)
- will the syntax be different ?


Slightly. The basic syntax is the same. The main difference is that a
PCRE pattern has to be enclosed in delimiters, i.e. a single char before
and after the pattern. The ending delimiter may then be followed by some
modifiers.

Micha
Jul 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Steve | last post by:
Hello, I am writing a script that calls a URL and reads the resulting HTML into a function that strips out everthing and returns ONLY the links, this is so that I can build a link index of various...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
3
by: | last post by:
Hi I need to write one regex to read all the fields from the following lines / file format line 1 - some_alphanumeric,some_alphanumeric,"something, something",numbers_hyphenatedORnot line 2 -...
18
by: yawnmoth | last post by:
Say I have the following script: <? $string = 'test'; if (eregi("^+$",$string)) { echo 'matches!'; } else {
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
3
by: gast128 | last post by:
Dear all, I was looking for a c++ implementation of regular expressions using the Interpreter Design Pattern. GOF only references a Smaltalk implementation which I can not read :(. Does...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.