jcd Posted November 28, 2007 Share Posted November 28, 2007 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. Quote Link to comment Share on other sites More sharing options...
effigy Posted November 28, 2007 Share Posted November 28, 2007 /\x20{2,}/ Only spaces? What about whitespace in general (tabs, new lines)? Quote Link to comment Share on other sites More sharing options...
jcd Posted November 28, 2007 Author Share Posted November 28, 2007 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. Quote Link to comment Share on other sites More sharing options...
effigy Posted November 28, 2007 Share Posted November 28, 2007 You can run two separate patterns, or combine them: /\A(?:\w|\x20(?!\x20))+\z/ Quote Link to comment Share on other sites More sharing options...
jcd Posted November 28, 2007 Author Share Posted November 28, 2007 Thanks. I don't understand why it works though. I have some reading to do Quote Link to comment Share on other sites More sharing options...
effigy Posted November 28, 2007 Share Posted November 28, 2007 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 ---------------------------------------------------------------------- Quote Link to comment Share on other sites More sharing options...
jcd Posted November 28, 2007 Author Share Posted November 28, 2007 Thanks, I haven't got to looking ahead yet, although I had heard of it. What tool did you use to break down the regexp? Quote Link to comment Share on other sites More sharing options...
effigy Posted November 28, 2007 Share Posted November 28, 2007 A Perl script: use warnings; use strict; use YAPE::Regex::Explain; my $regex = qr/\A(?:\w|\x20(?!\x20))+\z/; print YAPE::Regex::Explain->new($regex)->explain(); Quote Link to comment Share on other sites More sharing options...
jcd Posted November 28, 2007 Author Share Posted November 28, 2007 Thanks once more. It took some googling but the script works great Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.