terungwa Posted October 25, 2014 Share Posted October 25, 2014 I have a regex that validates a string. I want to ensure there is no space in the string. Thus far I have used the negative look ahead regex construct as shown below to match strings without spaces. (?!.*(\s)) While this prevents space in between a word, all strings with space characters at the end or beginning are getting validated. I do not want that at all. This is the full regex script below: $string = "#JebiamgoOeing0"; if (preg_match('/^.*(?=.{8,})(?!.*([A-Za-z0-9])\1{1})(?=.*[a-z])(?=.*[A-Z])(?!.*(\s))(?=.*[\d])(?=.*[\W]).*$/', $string)) { "do something"; } else { "do something else"; } I need help in resolving this. Thank you. Link to comment https://forums.phpfreaks.com/topic/292058-prevent-a-space-beginning-middle-or-end-charater-in-a-string-with-regex/ Share on other sites More sharing options...
terungwa Posted October 25, 2014 Author Share Posted October 25, 2014 I have fixed this, so no need to respond. It was a simple synthax error in my script!! Link to comment https://forums.phpfreaks.com/topic/292058-prevent-a-space-beginning-middle-or-end-charater-in-a-string-with-regex/#findComment-1494734 Share on other sites More sharing options...
Psycho Posted October 25, 2014 Share Posted October 25, 2014 That looks like an expression to verify the complexity of a password. Why would you not want to allow spaces? Using a passphrase is much better than a password and should be encouraged. Link to comment https://forums.phpfreaks.com/topic/292058-prevent-a-space-beginning-middle-or-end-charater-in-a-string-with-regex/#findComment-1494739 Share on other sites More sharing options...
terungwa Posted October 26, 2014 Author Share Posted October 26, 2014 That looks like an expression to verify the complexity of a password. Why would you not want to allow spaces? Using a passphrase is much better than a password and should be encouraged. Thank you Psycho for the input. This is a learning exercise and I needed to be able to test my regex skills in implementing every permutation I could think of:) Link to comment https://forums.phpfreaks.com/topic/292058-prevent-a-space-beginning-middle-or-end-charater-in-a-string-with-regex/#findComment-1494775 Share on other sites More sharing options...
terungwa Posted October 26, 2014 Author Share Posted October 26, 2014 I have fixed this, so no need to respond. It was a simple synthax error in my script!! This is the code snippet that fixed this. (?!.*(\s)) //Assert a string does not contain any white space characters after it using negative lookahead: ((?<!(\s)).*) //Assert a string does not contain any no white space characters before it using negative look behind: Link to comment https://forums.phpfreaks.com/topic/292058-prevent-a-space-beginning-middle-or-end-charater-in-a-string-with-regex/#findComment-1494776 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.