Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression which returns false always [duplicate]

Tags:

regex

php

Possible Duplicate:
What regular expression can never match?

how do i write a regular expression which returns false always in php.

i wanted this bcos . i wanted to display a error msg with out a form rule...so i did like this..

if($values['result_msg'] == 'ERROR')
                    {
                    $form->registerRule('just_check','regex','/$^/');                       
                    $form->addRule('abc', 'Please enter valid Model Number.','just_check');
                    }
like image 325
Hacker Avatar asked Aug 31 '10 13:08

Hacker


2 Answers

There are lots of ways to do it:

  • /(?=a)b/

This fails to match because it searches for a character which is both a and b.

  • /\Zx\A/

This fails to match because the end of the string cannot come before the start of the string.

  • /x\by/

This fails to match because a word boundary cannot be between the characters x and y.

like image 120
Mark Byers Avatar answered Sep 30 '22 15:09

Mark Byers


I don't know why you want to do this, but this'll do it:

(?!x)x

The first bit (?!..) is a negative lookahead which says "make sure this position does not match the contents of this lookahead", where the contents is x, and then the final x says "match x" - since these two are opposites, the expression will never match.

You may also want to add start/end markers, i.e.

^(?!x)x$

You can swap both the x with pretty much anything, so long as both bits are equivalent.

There are plenty of other ways to do it, basically you just put two mutually exclusive conditions next to each other for matching the same place, and the regex will fail to match - see Mark's answer for more examples.

like image 38
Peter Boughton Avatar answered Sep 30 '22 14:09

Peter Boughton