Suppose I want to take a short variable indexed text and match it to a longer indexed text, but I only want it to match if it comprises an entire word in the longer text. So if my text is
I’d like “axolotl” to match but not “x”. Something like this:
After reading a command:
if "axolotl barn swallow" matches [as a word] the text "[the player's command]": [UPDATE: earlier I left out the square brackets around "the player's command," which makes nonsense of the example.]
say "No match.";
reject the player's command.
Except with some restriction that looks only at words. If I were checking against a fixed text I could use a regular expression (if “axolotl barn swallow” matches the regular expression “\bx\b”), but can I put a variable indexed text in the middle of a regexp?
Sorry if there’s an obvious solution in the docs that I’m missing.
I believe you can’t since square brackets in regular expressions are interpreted as character ranges.
However, the sheer power of regular expression language means that you should be able to duplicate the effect of putting a variable indexed text in the middle of a regular expression just by using regular expression code.
To decide whether (string1 - indexed text) matches wordwise (string2 - indexed text):
repeat with n running from 1 to the number of punctuated words in string1:
if punctuated word number n in string1 exactly matches the text string2, decide yes;
It only will work if string1 is a single word, but that’s the case I’m looking for here.
UPDATE: By the way, now that I have two ways to do this (one with the “matching wordwise” thing I posted above, one with matching the regular expression “\b[the text substitution I want]\b”), is one of them quicker or otherwise preferable? I could be calling this a lot.
The regular expression engine treats them equally, but string literals must first be converted to indexed text, and square brackets have a different meaning at that stage. You could get past this by giving a regexp like “a vowel is [bracket]aeiou[close bracket]”.