barkster Posted February 25, 2009 Share Posted February 25, 2009 I have a string that can be any of the following want to make sure it is formatted correctly where if there is more than one entry they are separated by a semicolon and a space and the last entry does not have a semicolon. I cannot figure out how to do this, right now I'm just doing this. So essentially it can either be 3digit alpha or 3-digit alpha plus semicolon and space? Formats Accepted BBB BBB; BBB BBB; BBB; BBB; BBB regex using now ^([A-Za-z; ])*$ Link to comment https://forums.phpfreaks.com/topic/146875-formatting-question-with-semicolons/ Share on other sites More sharing options...
effigy Posted February 25, 2009 Share Posted February 25, 2009 <pre> <?php $tests = array( 'BBB', 'BBB; BBB', 'BBB; BBB; BBB; BBB', 'B;', ';B', ';;', ); foreach ($tests as $test) { echo "$test => "; echo preg_match('/\A([^;]+;\s*)*[^;]+\z/', $test) ? 'OK' : 'Not OK' ; echo '<br>'; } ?> </pre> Link to comment https://forums.phpfreaks.com/topic/146875-formatting-question-with-semicolons/#findComment-771130 Share on other sites More sharing options...
barkster Posted February 25, 2009 Author Share Posted February 25, 2009 I forgot to mention it can only be 3-digit, so only 3-digit alpha or 3-digit alpha semicolon space don't know how to make this as an or but need to combine these two or something simlilar ^([A-Za-z]{3})* ^([A-Za-z]{3};\s)* Link to comment https://forums.phpfreaks.com/topic/146875-formatting-question-with-semicolons/#findComment-771144 Share on other sites More sharing options...
nrg_alpha Posted February 25, 2009 Share Posted February 25, 2009 Based off of Effigy's snippet, perhaps: $tests = array( 'BBB', 'BBB; BBB', 'BBB; BBB; BBB; BBB', 'B;', ';B', ';;', ); foreach ($tests as $test) { echo "$test => "; echo preg_match('#\A([a-z0-9]{3}(?:; )?)+\z#i', $test) ? 'OK' . "<br />\n" : 'Not OK' . "<br />\n" ; } Output: BBB => OK BBB; BBB => OK BBB; BBB; BBB; BBB => OK B; => Not OK ;B => Not OK ;; => Not OK Is that what you are looking for? Link to comment https://forums.phpfreaks.com/topic/146875-formatting-question-with-semicolons/#findComment-771148 Share on other sites More sharing options...
barkster Posted February 25, 2009 Author Share Posted February 25, 2009 yes that is it, thanks. It was the question mark that I didn't know how to use. Optional instead of or. Thanks! Link to comment https://forums.phpfreaks.com/topic/146875-formatting-question-with-semicolons/#findComment-771161 Share on other sites More sharing options...
nrg_alpha Posted February 25, 2009 Share Posted February 25, 2009 I just realized that if it is only a format you are checking (in other words, you won't need to do any capturing at all), my initial brackets should be converted to non-capturing, much like the optional ;space part... Pattern: '#\A(?:[a-z0-9]{3}(?:; )?)+\z#i' Link to comment https://forums.phpfreaks.com/topic/146875-formatting-question-with-semicolons/#findComment-771196 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.