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. Link to comment https://forums.phpfreaks.com/topic/79305-solved-newbie-disallow-consecutive-characters/ 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)? Link to comment https://forums.phpfreaks.com/topic/79305-solved-newbie-disallow-consecutive-characters/#findComment-401429 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. Link to comment https://forums.phpfreaks.com/topic/79305-solved-newbie-disallow-consecutive-characters/#findComment-401460 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/ Link to comment https://forums.phpfreaks.com/topic/79305-solved-newbie-disallow-consecutive-characters/#findComment-401464 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 Link to comment https://forums.phpfreaks.com/topic/79305-solved-newbie-disallow-consecutive-characters/#findComment-401474 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 ---------------------------------------------------------------------- Link to comment https://forums.phpfreaks.com/topic/79305-solved-newbie-disallow-consecutive-characters/#findComment-401484 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? Link to comment https://forums.phpfreaks.com/topic/79305-solved-newbie-disallow-consecutive-characters/#findComment-401499 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(); Link to comment https://forums.phpfreaks.com/topic/79305-solved-newbie-disallow-consecutive-characters/#findComment-401506 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 Link to comment https://forums.phpfreaks.com/topic/79305-solved-newbie-disallow-consecutive-characters/#findComment-401515 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.