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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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

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.