Jump to content

[SOLVED] Is this the best way to do it?


micah1701

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

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)

 

regular_expressions.png

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.