# matching indexed text as a word

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 "Match!"; otherwise: 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.

Hope this helps.

How would I go about doing this? Regular expression language doesn’t give me any way to read out the value of my indexed text variable.

OK, I think this works:

```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; decide no. ```

It only will work if string1 is a single word, but that’s the case I’m looking for here.

No, I7 regexp use angle brackets for this, for exactly this reason. Square brackets are interpolated first.

if foo matches the regular expression “\b[var]\b”: …

…should work.

(If the variable contains any regexp-special characters, you’d have to escape those first.)

EDIT-ADD: Yes, I just tested this.

They why does it say this?

I’ll file a documentation bug.

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.

That paragraph:

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]”.

The regexp should be faster. The “matching wordwise” phrase will have to word-split the text over and over as it goes through the loop.

Thanks!

From the bug report, here’s dannii’s suggestion for a clarification:

I guess it’s safe to just use angle brackets for ranges and square brackets for text substitutions (especially since I don’t have any ingrained habits of using square brackets for ranges).