micah1701 Posted February 4, 2008 Share Posted February 4, 2008 I'm new to writing my own regex statement (I've always relied on the kindness of strangers) but today I needed to find a way to validate international Amateur radio callsigns. exciting huh? This is basically what I have ( i tried to explain the criteria a bit in the code itself) <?php /* a callsign basically has three parts: 1) alpha-numeric prefix which could be one of these combinations... A-Z{1,3} // between one and three letters [A-Z][0-9] // one letter followed by one number [A-Z][0-9][A-Z] // one letter, one number, one letter [0-9]([A-Z]{1,3}) // one number followed by between one and three letters 2) a single number: [0-9] 3) followed by between one and three letters: [A-Z]{1,3} */ function validate_callsign($callsign){ $regex = "[0-9]?"; // first character of prefix MAY be a number (but usually isn't) $regex.= "[A-Z]{1,2}"; // Must start with 1 or 2 Letters $regex.= "[0-9]"; //followed by 1 number $regex.= "[A-Z][0-9]?"; //there MAY be a char and number here IF the 2nd charecter of the prefix was a number $regex.= "[A-Z]{1,3}"; //and 1 to 3 more letters if(preg_match("/".$regex."/i",$callsign) ){ return true; } } valid callsigns could include: A1A AA1A AAA1AAA A1A1A 1A1A 1AA1AA It seems to be working as it should however i'm a little concerned about that 2nd to last line of the $regex statement: $regex.= "[A-Z][0-9]?"; Any thoughts from an expert? Quote Link to comment Share on other sites More sharing options...
obsidian Posted February 4, 2008 Share Posted February 4, 2008 As it is, you seem to have the basic logic down, but you're not anchoring your string, so if one were to start with "***" and then a valid call sign, your pattern would match. Remember that when you need an exact match, you should start your match with "^" and end it with "$": $pattern = '/^(\d?[a-z]{1,3}|[a-z]\d[a-z]?)\d[a-z]{1,3}$/'; Hope that helps some. Quote Link to comment Share on other sites More sharing options...
effigy Posted February 4, 2008 Share Posted February 4, 2008 The multi-line comments seem to disagree with the pattern. Which is correct? Quote Link to comment Share on other sites More sharing options...
micah1701 Posted February 4, 2008 Author Share Posted February 4, 2008 The multi-line comments seem to disagree with the pattern. Which is correct? the comments are what it SHOULD be doing. the function is what I'm not sure about. thanks :-) Quote Link to comment Share on other sites More sharing options...
micah1701 Posted February 4, 2008 Author Share Posted February 4, 2008 As it is, you seem to have the basic logic down, but you're not anchoring your string, so if one were to start with "***" and then a valid call sign, your pattern would match. Remember that when you need an exact match, you should start your match with "^" and end it with "$": $pattern = '/^(\d?[a-z]{1,3}|[a-z]\d[a-z]?)\d[a-z]{1,3}$/'; Hope that helps some. that actually totally helped! I never understood the point of the anchors before ('cause I didn't know what anchors were). Thanks! like i said, i've been putting off learning this stuff for way to long. I can't rely on google+cut+paste forever. Thanks for your help. Quote Link to comment Share on other sites More sharing options...
effigy Posted February 4, 2008 Share Posted February 4, 2008 <pre> <?php $tests = array ( 'A1A', 'AA1A', 'AAA1AAA', 'A1A1A', '1A1A', '1AA1AA', ); function validate_callsign($callsign){ $regex = '/ \A ### BOL (?: \d? ### Optional digit [A-Z]{1,3} ### 1-3 letters | ### OR [A-Z] ### 1 letter \d ### 1 digit [A-Z]? ### Optional letter ) \d ### Digit [A-Z]{1,3} ### 1-3 letters \z ### EOL /xi'; if (preg_match($regex, $callsign)) { return true; } } foreach ($tests as $test) { echo $test, ' -- ', (validate_callsign($test) ? 'Valid' : 'Invalid'), '<br/>'; } ?> </pre> Quote Link to comment Share on other sites More sharing options...
micah1701 Posted February 4, 2008 Author Share Posted February 4, 2008 thanks Effigy. thats very awesome. I'm assuming the colon after the question mark means mean that one of the cases must match? Also, what is the reason for excaping the A at the begining? Thanks again for your help, I'm so glad I'm finally beginning to wrap my head around regular expression. (now if i could just learn spanish) Quote Link to comment Share on other sites More sharing options...
laffin Posted February 4, 2008 Share Posted February 4, 2008 One of the best tools I have found for regular expressions is Expresso, try googl'n it Quote Link to comment Share on other sites More sharing options...
effigy Posted February 5, 2008 Share Posted February 5, 2008 I'm assuming the colon after the question mark means mean that one of the cases must match? (?:regex) -- Non-capturing parentheses group the regex so you can apply regex operators, but do not capture anything and do not create backreferences. Source Also, what is the reason for excaping the A at the begining? \A -- Matches at the start of the string the regex pattern is applied to. Matches a position rather than a character. Never matches after line breaks. Source I love that image Thanks for sharing. 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.