Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

help with regular expression interpretation

 
   Database Forums (Home) -> PHP RSS
Next:  First Data Access page works great internally; ca..  
Author Message
hq4ever

External


Since: Aug 01, 2004
Posts: 1



(Msg. 1) Posted: Sun Aug 01, 2004 2:55 pm
Post subject: help with regular expression interpretation
Archived from groups: comp>lang>php (more info?)

function testemail($email) {

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

return eregi($validEmailExpr, $email);
}

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

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

$email = "foo..bar@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 Smile

 >> Stay informed about: help with regular expression interpretation 
Back to top
Login to vote
zYm3N

External


Since: Aug 01, 2004
Posts: 1



(Msg. 2) Posted: Sun Aug 01, 2004 2:55 pm
Post subject: Re: help with regular expression interpretation [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Dnia Sun, 01 Aug 2004 21:03:44 +0000, hq4ever (at) 012 (dot) net (dot) il
spłodził(a):

 > function testemail($email) {
 >
 > $validEmailExpr =

 >
 > return eregi($validEmailExpr, $email);
 > }
 >

 > testmail($email); //return TRUE
 >

 > testmail($email); //return TRUE
 >

 > testmail($email); //return FALSE - why ??
"]([.]?[0-9"

zero or one '.'


 > 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
..:: >> <a rel="nofollow" style='text-decoration: none;' href="http://zymen.cjb.net" target="_blank">http://zymen.cjb.net</a> <<
..:: <a rel="nofollow" style='text-decoration: none;' href="http://zymen.cjb.net/cytowanie.html" target="_blank">http://zymen.cjb.net/cytowanie.html</a>

 >> Stay informed about: help with regular expression interpretation 
Back to top
Login to vote
Ian.H

External


Since: Feb 02, 2004
Posts: 69



(Msg. 3) Posted: Sun Aug 01, 2004 4:54 pm
Post subject: Re: help with regular expression interpretation [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Sun, 01 Aug 2004 20:49:48 +0200, zYm3N wrote:


[ re: e-mail syntax validation ]


 > Try to use:
 >



foo+bar@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
<a rel="nofollow" style='text-decoration: none;' href="http://digiserv.net/" target="_blank">http://digiserv.net/</a>
 >> Stay informed about: help with regular expression interpretation 
Back to top
Login to vote
maxim vexler

External


Since: Aug 02, 2004
Posts: 18



(Msg. 4) Posted: Sun Aug 01, 2004 7:53 pm
Post subject: Re: help with regular expression interpretation [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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 ?
 >> Stay informed about: help with regular expression interpretation 
Back to top
Login to vote
Ian.H

External


Since: Feb 02, 2004
Posts: 69



(Msg. 5) Posted: Sun Aug 01, 2004 10:53 pm
Post subject: Re: help with regular expression interpretation [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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
<a rel="nofollow" style='text-decoration: none;' href="http://digiserv.net/" target="_blank">http://digiserv.net/</a>
 >> Stay informed about: help with regular expression interpretation 
Back to top
Login to vote
Ian.H

External


Since: Feb 02, 2004
Posts: 69



(Msg. 6) Posted: Sun Aug 01, 2004 11:53 pm
Post subject: Re: help with regular expression interpretation [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Mon, 02 Aug 2004 02:47:31 +0000, Ian.H wrote:

 > if (!preg_match(

 > $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
<a rel="nofollow" style='text-decoration: none;' href="http://digiserv.net/" target="_blank">http://digiserv.net/</a>
 >> Stay informed about: help with regular expression interpretation 
Back to top
Login to vote
Tim Van Wassenhove

External


Since: May 08, 2004
Posts: 134



(Msg. 7) Posted: Mon Aug 02, 2004 1:53 am
Post subject: Re: help with regular expression interpretation [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article , Ian.H wrote:
 > On Mon, 02 Aug 2004 02:47:31 +0000, Ian.H wrote:
 >
  >> if (!preg_match(

  >> $addy
  >> )) {
  >> return false;
  >> }
 >
 >
 > Oops, to fix the very issue you mentioned, change the regex to:
 >
 >



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
<a rel="nofollow" style='text-decoration: none;' href="http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html" target="_blank">http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html</a>

--
Tim Van Wassenhove <http://home.mysth.be/~timvw>
 >> Stay informed about: help with regular expression interpretation 
Back to top
Login to vote
maxim vexler

External


Since: Aug 02, 2004
Posts: 18



(Msg. 8) Posted: Mon Aug 02, 2004 5:54 am
Post subject: Re: help with regular expression interpretation [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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:

 >
  >>On Mon, 02 Aug 2004 02:47:31 +0000, Ian.H wrote:
  >>
  >>
   >>> if (!preg_match(

   >>> $addy
   >>> )) {
   >>> return false;
   >>> }
  >>
  >>
  >>Oops, to fix the very issue you mentioned, change the regex to:
  >>
  >>

 >
 >
 >
 > 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
<font color=purple> > <a rel="nofollow" style='text-decoration: none;' href="http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html</font" target="_blank">http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html</font</a>>
 >
 >> Stay informed about: help with regular expression interpretation 
Back to top
Login to vote
Ian.H

External


Since: Feb 02, 2004
Posts: 69



(Msg. 9) Posted: Mon Aug 02, 2004 6:53 am
Post subject: Re: help with regular expression interpretation [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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
<a rel="nofollow" style='text-decoration: none;' href="http://digiserv.net/" target="_blank">http://digiserv.net/</a>
 >> Stay informed about: help with regular expression interpretation 
Back to top
Login to vote
Michael Fesser

External


Since: Jul 05, 2004
Posts: 107



(Msg. 10) Posted: Tue Aug 03, 2004 1:54 pm
Post subject: Re: help with regular expression interpretation [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

.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
 >> Stay informed about: help with regular expression interpretation 
Back to top
Login to vote
maxim vexler

External


Since: Aug 02, 2004
Posts: 18



(Msg. 11) Posted: Wed Aug 04, 2004 6:54 pm
Post subject: Re: help with regular expression interpretation [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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
 >> Stay informed about: help with regular expression interpretation 
Back to top
Login to vote
Michael Fesser

External


Since: Jul 05, 2004
Posts: 107



(Msg. 12) Posted: Thu Aug 05, 2004 10:55 am
Post subject: Re: help with regular expression interpretation [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

.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
 >> Stay informed about: help with regular expression interpretation 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
regular expression - I want to check for regular expression a text description field, say 250 caracters max.. The field can contain only letters, numbers, comma and dot. What will be the correct ereg php reg expression for this? Can't figure out the proper expression. ..

Regular expression - I want to remove everything from a string except for Numbers,Lettters and Hypens. Can't get it working, can anyone help? Thanks,

Regular expression - Hello I need a regular expression that validate a list of numbers separated by "-" , numbers can not be greater than 999 Valid examples 0 12-455-01 1-9 125-32-155-45-45 Invalid examples -1 45- 1-45665456-4 12-45- - Thanks ;)

regular expression inquiry - I have a text file (see below please) to parse with the following code. i don't know why it cannot match anything. thanks. $data=fread(fopen("$dir/$file","r"),filesize("$dir/file")); $matc...

str_replace & regular expressions question - Hi folks, I am just writing to ask for help with something i'm trying to do with a string. I have a filename, for instance 'file.txt'. However i'd like to make a script to take that file name and return it as something like: <a href=..
   Database Forums (Home) -> PHP All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]