473,584 Members | 2,819 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with regular expression interpretation


function testemail($emai l) {

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

return eregi($validEma ilExpr, $email);
}

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

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

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

$email = "foo.@bar.gov.m il";
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 1904
Dnia Sun, 01 Aug 2004 21:03:44 +0000, hq4ever (at) 012 (dot) net (dot) il
spłodził(a):
function testemail($emai l) {

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

return eregi($validEma ilExpr, $email);
}

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

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

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

zero or one '.'
$email = "foo.@bar.gov.m il";
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_reco rds = 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_rec ords) ? $mx_records : true;
} else {
unset($mx_recor ds);
return false;
}
}

USAGE:

if (!validate_emai l($_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.dig iserv.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.dig iserv.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

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

Similar topics

9
3686
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 pages. I have been programming in PHP for over 2 years now and have never encountered a problem like the one I am having now. To me this seems...
1
4158
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 regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) ...
5
2512
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, and then reconstruct another URL based on it. For example, I need to scan a web page looking for something like <a...
4
3215
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 over each document, find out if it contains a header and/or a footer and extract only the main content part. The headers and the footers have no...
3
1446
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 - some_alphanumeric,some_alphanumeric,something something,numbers_hyphenatedORnot At first I thought this one will do
18
2015
by: yawnmoth | last post by:
Say I have the following script: <? $string = 'test'; if (eregi("^+$",$string)) { echo 'matches!'; } else {
1
3700
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 attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am...
3
2554
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 expression. ^(.+?) uses (?!a spoon)\.$
3
2336
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 anyone know a link besides the cuj january 2003 article ('A Different Interpretation of the Interpreter Design Pattern'). I am especially stuck with the...
0
7897
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7829
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8189
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7940
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8198
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5705
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2336
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1438
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.