Match user input to pattern

Hey new to the forum and AppleScript so probably a stupid question. All I need to do is take some user input and verify it meets my criteria. The criteria are P + 2 numbers + “-” + 4 numbers (ie P01-2345) or NS and 7 numbers (ie NS0123456). What I have is:

property theCommand : ""

repeat while theCommand is ""
	display dialog "User Input:" default answer theCommand
	set theCommand to text returned of result
	display dialog "Is" & theCommand & "correct?"
	if theCommand is not equal to "P" &  {[0 - 9], {2}} & "-" ([0-9]{4}) then
		display dialog "That doesn't match P01-2345"
		tell application "Safari"
			open location "http://website"
		end tell
		if theCommand is not equal to "NS" & {[0 - 9], {7}} then
			display dialog "Invalid input"
			set theCommand to ""
		end if
	end if
end repeat

Everything works but the part that matters. I can’t seem to figure out how to match the pattern in applescript. My google skills must be getting rusty. Any help is appreciated

The long way

set theCommand to ""

repeat while theCommand is ""
	display dialog "User Input:" default answer theCommand
	set theCommand to text returned of result
	display dialog "Is \"" & theCommand & "\" correct?"
	if theCommand does not start with "P" and ¬
		theCommand does not start with "NS" then
		display dialog "That doesn't match either \"P\" or \"NS\""
		set theCommand to ""
		--other things when above criteria not met
	else if length of (every character of theCommand) is not equal to 8 and ¬
		length of (every character of theCommand) is not equal to 9 then
		display dialog "Invalid entry"
		set theCommand to ""
		--other things when above critera not met
	else if (character 4 of theCommand) is "-" and ¬
		(my checkNumeric(characters 2 thru 3 of theCommand as string) is false or ¬
			my checkNumeric(characters 5 thru -1 of theCommand as string) is false) then
		display dialog "That doesn't match \"P01-2345\""
		set theCommand to ""
		--other things when above critera not met
	else if theCommand starts with "NS" and ¬
		my checkNumeric(characters 3 thru -1 of theCommand as string) is false then
		display dialog "That doesn't match \"NS0123456\""
		set theCommand to ""
		--other things when above critera not met	
	end if
end repeat


on checkNumeric(aString)
	try
		aString as integer
		return true
	on error
		return false
	end try
end checkNumeric

I’ve set theCommand initially rather than declare it as a property, because if it’s a property the repeat does not run again once the user enters correct data - a property stores it like a preference, so if it’s not “” before it gets to the repeat while… step (such as running it again), the script does nothing. I’ve also put a numeric check in a subroutine so that I don’t have to write the same thing over and over, nor check each character one at a time to see if it’s numeric (“as integer” preferred over “as number” because 1.2 is a number but doesn’t meet your definition of being numeric).

The short way is to use regex like you are trying to use, but it will require using “do shell script” or a 3rd party osax since AppleScript doesn’t have any built-in regex.

Thank you! No wonder I couldn’t find anything about regex and AppleScript. I appreciate the help.