Gossamer Forum
Home : General : Perl Programming :

PHP Regex....

Quote Reply
PHP Regex....
Mmm. I've been having a weird problem with this regex!

I'm using preg_match, which should just match a string.

I'm using;

Code:
if (!preg_match("/^[0-9a-zA-Z_.-]*@[0-9A-Za-z_.].[a-zA-Z]{2,4}/",$email)) { error("Email address $email does not seem to be valied!"); }


However, if I add a - in, as such;

Code:
if (!preg_match("/^[0-9a-zA-Z_.-]@[0-9A-Za-z_.-].[a-zA-Z]{2,4}/",$email)) { error("Email address $email does not seem to be valied!"); }

If I escape it with a \, it still doesn't match!

It will match all of these combinations WITH the - there, just not escaped;

my-webmaster@ewee.com
webmaster@mydomain.com
webmast@something.ws
some-thing@somewhere.co.uk

Ones that don't work;

my-webmaster@ewe-oioe.com
someone@my-domain.com
and anything with an - after the @ sign!

Anyone had experience with this regex before, and if so, any ideas why its doing that? Unimpressed

Thanks

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] PHP Regex.... In reply to
Your top and bottom regexs aren't the same. The second one has a possible problem which is [b]

Try this:

!preg_match("/^\S+@\S+\.\S+/",$email)

Im not sure if regex works the same in php but if so then above should work.

Last edited by:

RedRum: Jan 16, 2002, 7:29 AM
Quote Reply
Re: [RedRum] PHP Regex.... In reply to
Argh...that shouldn't have come out like that! For some reason it has made [b]-[/ b] come out as [ b.]- Unimpressed Possibly a bug (I tried to edit it..but for some reason no matter how i edit it I can't get it to come up bold!).

So, its not that [b.] part that is wrong with the code Wink

Ok, what does \S stand for? PHP Regex is very much like Perls. In fact I belive that it was based of Perl regex system Tongue

Thanks

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] PHP Regex.... In reply to
\S will matching everything but whitespace.

If you don't want that you can use:

/[\w\.-]+@[^\.]+\.\w+([\w\.]+)?/i

...well just a quick attempt :)

Last edited by:

RedRum: Jan 16, 2002, 7:53 AM
Quote Reply
Re: [RedRum] PHP Regex.... In reply to
Ok, thanks. I'll have a look at that Smile

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] PHP Regex.... In reply to
I edited the regex above just incase you didn't notice.