Jump to content

Negating Regex


doubledee

Recommended Posts

Why not try it and see?

Because I am trying to get my "reset_password.php" working and am stuck deep in my code...

 

You can always create a new fresh file just to test a particular thing, especially something as simple as this.

 

<?php

$value='198248';
$regex='/^\d+$/';
if (!preg_match($regex, $value)){
  echo 'Hey, it works!';
}
else {
   echo 'Oops';
}

 

 

That said, ! means not so if you have a function that tests a condition, and you want to know if that condition is not true you can just prefix it with ! to reverse the returned boolean value.  !true = false,  !false = true.

Link to comment
Share on other sites

Then try it and see.

 

Wow, all of my problems are solved!

 

I'll never have to post here again, because all I have to do is test my code!

 

 

Debbie

 

 

Why would you not test your code? That is the best way to learn programming IMO, trial and error. I probably have ~50 random PHP files floating around my local dev box where I have tested various things.

 

Furthermore if your code is structured properly, even if it is very complex code, it is a lot easier to debug such things as this.

 

For the record, any statement or function that returns true or false can be negated with the ! character. If you look on the PHP manual for preg_match(), you can see its return value:

preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject. preg_match() returns FALSE if an error occurred.

 

So if there is no match preg_replace() will return 0. Since 0 is evaluated as false in PHP, you can simply negate it with the !. If preg_match() fails due to an error, however, it will return boolean false. Depending on your circumstances you may want to handle both cases. So therefore you could do something like...

if (preg_match() === 0) {
    // no matches
} else if (preg_match() === false) {
    // the match failed due to an error
} else {
    // matches!
}

Link to comment
Share on other sites

Why would you not test your code?

 

Because I have 300+ lines of broken code because everyone tells me to go left and then right and I didn't want to write something from scratch just to test this particular issue, so I was hoping someone would throw me a bone and just say "Yes it'll work" or "No, it doesn't work that way".

 

I spend all freaking day reading the manual and testing my code, so when I'm stuck and having larger issues, I don't appreciate the Mods crying and giving me warnings when this was a simple Yes/No thread.

 

 

For the record, any statement or function that returns true or false can be negated with the ! character.

 

Well I wasn't going to assume that with a Regex.

 

(I have assumed before and got burned, which is why I come here to ask the experts.)

 

Just because code works does not mean it is programmed correctly and will always work...

 

 

Debbie

 

Link to comment
Share on other sites

Then try it and see.

 

Wow, all of my problems are solved!

 

I'll never have to post here again, because all I have to do is test my code!

 

 

Debbie

 

 

That would be refreshing.

 

I don't recall asking for your help or commentary either.

 

If you don't like helping me then don't.

 

 

Debbie

 

 

Link to comment
Share on other sites

Why would you not test your code?

 

Because I have 300+ lines of broken code because everyone tells me to go left and then right and I didn't want to write something from scratch just to test this particular issue, so I was hoping someone would throw me a bone and just say "Yes it'll work" or "No, it doesn't work that way".

 

You don't have to test it in your "300+ lines of broken code", simply create a new file and test it there. It takes far less time and effort than to post a thread and wait for a response.

 

I spend all freaking day reading the manual and testing my code, so when I'm stuck and having larger issues, I don't appreciate the Mods crying and giving me warnings when this was a simple Yes/No thread.

 

 

For the record, any statement or function that returns true or false can be negated with the ! character.

 

Well I wasn't going to assume that with a Regex.

 

Why not? It is simply a function like any other. False is false, it doesn't matter where it came from.

Link to comment
Share on other sites

For the record, any statement or function that returns true or false can be negated with the ! character.

 

Well I wasn't going to assume that with a Regex.

 

Why not? It is simply a function like any other. False is false, it doesn't matter where it came from.

 

Because when it comes to things like 0 and FALSE and == and === things are not always as them seem.

 

If I trusted the outcome based on my knowledge I obviously wouldn't post here.

 

When I post here it is because I either 1.) Don't know how to do something, 2.) Don't trust my knowledge, or 3.) Want other people's opinions on choosing a better approach.

 

I'm a newbie - and not an expert like you all - so I require more hand-holding.

 

 

Debbie

 

 

Link to comment
Share on other sites

For the record, any statement or function that returns true or false can be negated with the ! character.

 

Well I wasn't going to assume that with a Regex.

 

Why not? It is simply a function like any other. False is false, it doesn't matter where it came from.

 

Because when it comes to things like 0 and FALSE and == and === things are not always as them seem.

 

If I trusted the outcome based on my knowledge I obviously wouldn't post here.

 

When I post here it is because I either 1.) Don't know how to do something, 2.) Don't trust my knowledge, or 3.) Want other people's opinions on choosing a better approach.

 

I'm a newbie - and not an expert like you all - so I require more hand-holding.

 

 

Debbie

 

Remember, this is a help forum, we are here to help you along the way, not do the work for you (not saying that you are asking someone to do the work for you in this case). We expect that you (a user of the forums) will give your best effort to find a solution to the problem before asking for help on this forum. Now this particular question you could have answered yourself by simply running a few test scripts and analyzing the results, as Pika said. Don't smack the hand that tries to hold yours.

Link to comment
Share on other sites

Because when it comes to things like 0 and FALSE and == and === things are not always as them seem.

 

They are exactly as they seem.

 

0 is an integer.

 

FALSE is a boolean.

 

In PHP, 0 evaluates to FALSE.

 

The ! operator checks to see if a statement is FALSE.

 

Therefore, if something returns 0 (or FALSE), the ! operator will work.

 

function returnsZero()
{
return 0;
}

function returnsFalse()
{
return false;
}

if (!returnsZero()) {
// passed
}

if (!returnsFalse()) {
// passed
}

if (returnsZero() == 0) {
// passed
}

if (returnsZero() == false) {
// passed
}

if (returnsZero() === 0) {
// passed
}

if (returnsZero() === false) {
// did NOT pass
}

if (returnsFalse() == 0) {
// passed
}

if (returnsFalse() == false) {
// passed
}

if (returnsFalse() === 0) {
// did NOT pass
}

if (returnsFalse() === false) {
// passed
}

 

Hope that clears it up.

 

Also, as a side note, you can run an interactive PHP shell from the command line by using the commands php -a. Then, you can execute any PHP commands. Therefore you don't even need to create a file, you can test code right there.

Link to comment
Share on other sites

... and I didn't want to write something from scratch just to test this particular issue, so I was hoping someone would throw me a bone and just say "Yes it'll work" or "No, it doesn't work that way".

 

I spend all freaking day reading the manual and testing my code, so when I'm stuck and having larger issues, I don't appreciate the Mods crying and giving me warnings when this was a simple Yes/No thread.

 

So, you simply wanted to waste our time when you could have simply written, what, a 5-7 line prototype and see for yourself?

 

Sorry, that doesn't fly.  Testing code and writing small use cases and prototypes comes with the territory.  Further, you've been here long enough to know that yes/no threads, especially when it's something you can answer yourself in mere minutes, are not acceptable here.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.