Amateur Radio Callsigns

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
gm1wkr
Forum Newbie
Posts: 3
Joined: Thu Jul 24, 2008 3:39 pm

Amateur Radio Callsigns

Post by gm1wkr »

Hi,

I'm trying to get a regex to match UK Ham Radio Callsigns which can be in the following formats - honestly I do not know where to start 8O.

Callsigns start with either G, M or 2. If the 1st character is G or M it can be followed with a number or M, W, I, J, U, B, Z, S, T, X. If the 1st character is 2 then the second could be E,M,W,I,J,U followed by another number (1 or 0).

After the second or third character (3rd if starts with 2, second or third if starts with M or G) there will be 1 to 3 letters.

The second character indicated the region of the UK, some examples
G1XXX - England
2e1XXX - England
M6xxx - England

Scotland - GM1XXX or 2M0XXX or MM3XXX
Wales - GW7XXX or 2W1XXX or MW0XXX
The other letters re[resent other regions.

The First number following G or M can be 0-9 as can the number following an initial 2.

I can't work out where to start untangling this and would appreciate any help from a REGEX guru.

The reason for this is to work out if a signup username is arbitary or a UK Callsign.

Kind regards
Mike GM1WKR
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Amateur Radio Callsigns

Post by prometheuzz »

Code: Select all

([GM][0-9MWIJUBZSTX]|2[EMWIJU][01])[A-Z]{1,3}
and in plain English:

Code: Select all

(
  [GM][0-9MWIJUBZSTX]   a G or M, followed by a number or M,W,I,J,U,B,Z,S,T,X
  |                     OR
  2[EMWIJU][01]         a 2 followed by E,M,W,I,J,U ending with a 0 or 1
)
[A-Z]{1,3}              one, two or three a capital letter(s)
By the way, you said "If the 1st character is G or M it can be followed with a number or M, W, I, J, U, B, Z, S, T, X", that would mean that "GW7XXX" is not correct.
gm1wkr
Forum Newbie
Posts: 3
Joined: Thu Jul 24, 2008 3:39 pm

Re: Amateur Radio Callsigns

Post by gm1wkr »

@prometheuzz Much appreciated!!

I'll try this out tonight. I downloaded Regex Coach so I'll now work to understand this. REGEX, I think, is one of those thing that seperates the men from the boys. :)

I came up with the following test after a few hours with Regex Coach and the tutes but it is not complete and I think the syntax is a bit bloated ,

Code: Select all

(g|m|2)(m|w|i|e|j|u){0,1}[0-9][a-z]{2,3}

update:
I think my original explanation may have been wrong, regarding the GM7XXX ... it should have been

A G or an M followed by EITHER a number OR M,W,I,J,U,B,Z,S,T,X and then a number
OR
a 2 Followed by a 2 followed by E,M,W,I,J,U ending with a 0 or 1
THEN
One, two or three letters.

Anyways thanks Again, your example has allowed me to work this out for myself (I think).

Again, many thanks,
Cheers
Mike
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Amateur Radio Callsigns

Post by prometheuzz »

gm1wkr wrote:@prometheuzz Much appreciated!!
You're welcome.
gm1wkr wrote:I'll try this out tonight. I downloaded Regex Coach so I'll now work to understand this. REGEX, I think, is one of those thing that seperates the men from the boys.
;)
I don't know. Some people call regex a strange form of Voodoo.
Makes me think of the following joke: "A programmer had a problem, which could be solved using regex. Now he had two problems."
gm1wkr wrote:I came up with the following test after a few hours with Regex Coach and the tutes but it is not complete and a bit bloated ,

Code: Select all

(g|m|2)(m|w|i|e|j|u){0,1}[0-9][a-z]{2,3}
Ah, I now see what you mean. A couple of remarks:
- when matching single characters, don't use the logical OR, but use character classes instead: so, not "(A|B|C)" but [ABC]. It's easier to read, and even (slightly) faster (for most regex engines). But the readability is more important in this case (all IMHO, of course!).
- the '?' in regex means 'zero or one time', so the regex "[ABC]{0,1}" is equivalent to "[ABC]?"

So, to shorten your regex, you could do:

Code: Select all

[gm2][mwieju]?[0-9][a-z]{2,3}
Note that your regex will not match capitalized matches. You will need to provide some sort of flag to the regex engine you're using to ignore cases (in PHP this can be done like this: '/your-regex/i', where the 'i' flag means ignore case), or you can extends your regex like this:

Code: Select all

[gmGM2][mwiejuMWIEJU]?[0-9][a-zA-Z]{2,3}
gm1wkr wrote:Again, many thanks,
Cheers
Mike
Again, no problem. By the looks of it, you'll be answering questions here soon!
;)
gm1wkr
Forum Newbie
Posts: 3
Joined: Thu Jul 24, 2008 3:39 pm

Re: Amateur Radio Callsigns

Post by gm1wkr »

@prometheuzz,

Thanks for that ;)

I'm going to spend some time on understanding this now, so your pointers are most welcomed and very helpful. I'v plugged this in to my script (PHP) and made it case insenative - it works well.

I was doing

Code: Select all

$str = strtolower($str)
but the / .. / i is far better and cleaner.

Answering questions here, well we will see. :lol:

I have to say that REGEX Coach is a fantastic peice of software ;) I'v always run away from REGEX (I'm a hobbyist nothing more) and really understand that joke ;) Maybe this will be a corner turned in my PHP learning!!!

Again, many thanks ;)
Cheers
Mike
Post Reply