Jump to content

[SOLVED] newbie: disallow consecutive characters


jcd

Recommended Posts

Hello. I have a test script below:

 

$string = 'my test string';

$pattern = '/^[\w ]*$/';

echo preg_match($pattern,$string) ? 'GOOD string' : 'BAD string';

 

I wish for any occurances of 2 or more consecutive spaces to be a bad string.

 

ie:

 

'my test' = GOOD

'my test string' = GOOD

'my          test string' = BAD

 

I have tried lots of brackets and negation (^) but can't figure it out ???

 

Thanks.

Yes I forgot about general whitespace, so I suppose the above regexp is now /\s{2,}/ ?

 

But the problem is that now "+" and "=" etc are allowed. I can't figure out how to combine above regexp with mine which only allows \w and single space characters.

NODE                    EXPLANATION

----------------------------------------------------------------------

  \A                      the beginning of the string

----------------------------------------------------------------------

  (?:                      group, but do not capture (1 or more times

                          (matching the most amount possible)):

----------------------------------------------------------------------

    \w                      word characters (a-z, A-Z, 0-9, _)

----------------------------------------------------------------------

  |                        OR

----------------------------------------------------------------------

    \x20                    character 32

----------------------------------------------------------------------

    (?!                      look ahead to see if there is not:

----------------------------------------------------------------------

      \x20                    character 32

----------------------------------------------------------------------

    )                        end of look-ahead

----------------------------------------------------------------------

  )+                      end of grouping

----------------------------------------------------------------------

  \z                      the end of the string

----------------------------------------------------------------------

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.