As I have multiple occurrences of those indicators within the script, my idea is to define your plugin string early in the script once, like shown here:
set theYesterdayIndicators to {"yes", "ges", "hie", "aye"} -- for yesterday, gestern, hier, ayer
set theTodayIndicators to {"tod", "heu", "auj", "hoy"} -- for today, heute, aujourd'hui, hoy
set theTomorrowIndicators to {"tom", "mor", "dem", "mañ", "man"} -- for tomorrow, morgen, demain, mañana, manana
set theDayIndicators to {"d", "t", "j"} -- for day/dÃa, Tag, jour
set theWeekIndicators to {"w", "s"} -- for week/Woche, semain/semana
set text item delimiters to ""
set validateDayWeekIndicator to (theDayIndicators as string) & theWeekIndicators as string
set text item delimiters to "|"
set validateYesTodTomIndicator to ((theYesterdayIndicators as string) & "|" & theTodayIndicators as string) & "|" & theTomorrowIndicators as string
set plugin to "[" & validateDayWeekIndicator & "]|(" & validateYesTodTomIndicator & ")[[:alpha:]]*"
display dialog plugin buttons {"Looks exactly as it should"}
on validateDateInput(aDate)
-- A string of the short-date separators to be recognised.
-- These will be used in a regex class, so the hyphen must be first or last.
set allowedSeparators to "-/."
-- A list of short-date part regexes, in day, month, year order.
set datePartRegices to {"(0?[1-9]|[12][0-9]|3[01])", "(0?[1-9]|1[0-2])", "([1-9][0-9])?[0-9]{2}"}
-- Get the local short-date string for 1st February 4003, strip out everything except the "1", the "2", and the "3", and turn these into a 3-digit integer. Use the digits to index the short-date part regexes and to arrange them into the equivalent order in a full short-date regex.
set order to (do shell script ("<<<" & quoted form of short date string of («data isot343030332D30322D3031» as date) & " sed -E 's/[^123]//g'")) as integer
set separatorClass to "[" & allowedSeparators & "]"
tell datePartRegices to set shortDateRegex to item (order div 100) & separatorClass & item (order mod 100 div 10) & separatorClass & item (order mod 10)
if ((do shell script ("<<<" & quoted form of aDate & " sed -E '/^" & shortDateRegex & "$/ !s/.*/false/; /false/ !s/.+/true/ ;'")) as boolean) then
-- If aDate is a valid date string in any of the allowed formats, test to see if it represents a valid date.
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to characters of allowedSeparators -- Requires Snow Leopard or later.
tell aDate's text items
set {item (order div 100), item (order mod 100 div 10), item (order mod 10)} to {beginning as integer, item 2 as integer, end as integer}
set {d, m, y} to it
end tell
set AppleScript's text item delimiters to astid
set ValidDate to ((d < 29) or (m is in {1, 3, 5, 7, 8, 10, 12}) or ((d < 31) and (m > 2)) or ((d is 29) and (y mod 4 is 0) and (y mod 400 is not in {100, 200, 300})))
else
-- Otherwise test for any of the alternative valid inputs. (It might be necessary to add foreign characters here - both lower and upper case - when adding further languages)
set ValidDate to (do shell script ("<<<" & quoted form of aDate & " sed -E 'y/ABCDEFGHIJKLMNÑOPQRSTUVWXYZ/abcdefghijklmnñopqrstuvwxyz/ ; /^([-+]?[1-9][0-9]*" & plugin & ")$/ !s/.*/false/; /false/ !s/.+/true/ ;'")) as boolean
end if
return ValidDate
end validateDateInput
set theDueDate to "2d"
if validateDateInput(theDueDate) of me is true then
say "true"
else
say "false"
end if
set plugin returns a string which is absolutely identical to “[dwtjs]|(yes|to[dm]|ges|heu|mor|hie|auj|dem|aye|hoy|man|mañ)[[:alpha:]]*”, the script however ends with an error, claiming plugin hasn’t been defined (number -2753 from “plugin”). What am I doing wrong?
UPDATE: Guess I just solved the problem: → of me was missing:
set ValidDate to (do shell script ("<<<" & quoted form of aDate & " sed -E 'y/ABCDEFGHIJKLMNÑOPQRSTUVWXYZ/abcdefghijklmnñopqrstuvwxyz/ ; /^([-+]?[1-9][0-9]*" & plugin of me & ")$/ !s/.*/false/; /false/ !s/.+/true/ ;'")) as boolean