Anti-Moronic Posted May 21, 2011 Share Posted May 21, 2011 I've have been trying for too long now to understand the regex required for this. I'd appreciate any help I can get. This should match: 012-345 69 However, these should not: -012345 678 01203- 34566 1234x567 This is validation is: 1) must not begin or end with dash (-), I have trimmed to ensure does not begin or end with space 2) each number may contain a dash or space in between - however, cannot contain any combination of the two in sequence. Like dash dash, dash space, space space, space dash 3) the only other characters which can be present in the number are dash and space. Any ideas? Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted May 21, 2011 Share Posted May 21, 2011 You can use trim to remove the dashes from either side too $new_subject = trim($subject, ' -'); To match that with regex then use if(preg_match('~^\d{3}-\d{3} \d{2}$~', $new_subject)) { // matched successfully code here } else { // Failed to match code here } This of course assumes you want to have the dash and space between the numbers exactly as you have it Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted May 21, 2011 Author Share Posted May 21, 2011 Thanks! Sadly, according to 2 your assumption would be incorrect. That is just an example of a match. A few more matches: 01-2-345 69 012 345 69 012345-69 This is the trouble I had. I am having to match based on each number and the adjacent character. Also, I cannot trim, this must run without trimming dashes. Any other ideas? Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted May 21, 2011 Share Posted May 21, 2011 I see. OK well this should satisfy those three points ~^(?!-)(\d[\s-]?){1,}$~ Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted May 21, 2011 Share Posted May 21, 2011 Oops, the dash at the end hadn't been accounted for, here's the proper one ~^(?!-)(\d[\s-]?){1,}(?<!-)$~ Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted May 21, 2011 Author Share Posted May 21, 2011 Thanks Jay, I really appreciate your input but those are just examples. I could send over a 100 examples - the main thing I am trying to validate is point 2) above. For example, this should also match: 012-345-69 even: 0-1-2-3-4-5-6-9 or 0 1 2 3-4-5-6-9 String cannot begin or start with dash. after each number there can only be a dash, space or another number, and after each dash or space there cannot be another dash or space. Thanks again. I've validated based on specific examples, but as soon as I try to write one which matches all I get stuck. Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted May 21, 2011 Author Share Posted May 21, 2011 Wait...I think this is working! Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted May 21, 2011 Share Posted May 21, 2011 This is what the last regex matches, (the ones in yellow/blue are valid) http://screencast.com/t/x9PmK3VOgB5 It seems to satisfy all of your above requirements, note that two dashes in a row won't work Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted May 21, 2011 Author Share Posted May 21, 2011 Yep, works great Thanks so much!! Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted May 21, 2011 Share Posted May 21, 2011 No problem Quote Link to comment Share on other sites More sharing options...
salathe Posted May 21, 2011 Share Posted May 21, 2011 ~^(?!-)(\d[\s-]?){1,}(?<!-)$~ An alternative approach would be something like the following, which might be easier to understand at-a-glance. /^\d([ -]?\d)*$/D Also note that the literal space character was used (since \s matches more than just the space character) as was the D modifier (which prevents values with trailing newlines from being false positives). 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.