!preg_match Regex not compiling

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I ran across this code, and it kind of made me nervous:  (as an email

if ( !preg_match("/.*\@.*\..*/", $_POST['email']) | preg_match("/(\)/",
$_POST['email']) )

1) from bitwise experience with "C", I was not at all comforable with
The manual is not clear about using  !  with a non-boolean value.
When using   !($var)   is  ($var)  converted to a boolean value, before
doing the NOT oper ?

2) I believe the 2nd use of  preg_match  was the one that caused a
REGEX compilation error.
Warning: preg_match() [function.preg-match]: Compilation failed:
missing ) at offset 3 in ..

can anyone help ?

Re: !preg_match Regex not compiling

Quoted text here. Click to load it

The missing ) is here: /(\)/
since you are using the backslash, which is an escape character, it escapes  
the ), but actually you should be using
so that it instead another backslashs escapes the backslash you wanted to  
use in the first place. It is a cumbersome thing but you'll get the hang of  
it I'm sure.

And fear not, all values are cast to boolean when you do boolean operations  
with them. Althouh I would recommend you to use || instead of | since you  
really aren't doing bitwise comparison here. But that's up to you really.

SETI @ Home - Donate your cpu's idle time to science.
Further reading at <http://setiweb.ssl.berkeley.edu/

Re: !preg_match Regex not compiling

Kimmo Laine wrote:
Quoted text here. Click to load it

actually, this regex is still broken. You start a group, but never  
finish it. If you wanted to match ) then it should be


Re: !preg_match Regex not compiling

Quoted text here. Click to load it

I assumed the searched term was "\" and not "\)". Because then the closing  
brace would have been there already, since it wasn't escaped. And when you  
think about it, if the search was indeed "\)", then they both need to be  
escaped "\)" and then surrounded with the grouping braces (\))... then  
finally /(\))/ . Way confusing... o_O

When regular expression is used to solve a problem, you'll only end up with  
having two problems instead of one.

SETI @ Home - Donate your cpu's idle time to science.
Further reading at <http://setiweb.ssl.berkeley.edu/

Re: !preg_match Regex not compiling

Quoted text here. Click to load it
Basically as long as that function does not return false it acts as if  
it is valid so therefore it is okay.

Although there is an error after the first preg match the single pipe  
should be a double pipe.

Quoted text here. Click to load it
Why would you want to use that regular expression anyhow?  It is not  
very well tailored. A better one might be:

, $email)) { ... }

You can also do validation techniques like getting the MX record and  
pinging the mail server to see if it actually accepts connections as  
well to help you get the integrity of the email address.


Site Timeline