Inform 7 Home Page / Documentation


§20.5. Matching and exactly matching

Up to now, we have only been able to judge two texts by seeing if they are equal, but we can now ask more subtle questions.

if (text) matches the text (text):

This condition is true if the second text occurs anywhere inside the first. Examples:

if "[score]" matches the text "3", ...

tests whether the digit 3 occurs anywhere in the score, as printed out; and

if the printed name of the location matches the text "the", ...

tests to see whether "the" can be found anywhere in the current room's name. Note that the location "Smotheringly Hot Jungle" would pass this test - it's there if you look. On the other hand, "The Orangery" would not, because "The" does not match against "the". We can get around this in a variety of ways, one of which is to tell Inform to be insensitive to the case (upper or lower) of letters:

if the printed name of the location matches the text "the", case insensitively: ...

if (text) exactly matches the text (text):

This condition is true if the second text matches the first, starting at the beginning and finishing at the end. This appears to be the same as testing if one is equal to the other, but that's not quite true: for example,

if "[score]" exactly matches the text "[best score]", ...

is true if the score and best score currently print out as the same text, which will be true if they are currently equal as numbers; but

if "[score]" is "[best score]", ...

is never true - these are different texts, even if they sometimes look the same.

In the next section we shall see that "matches" and "exactly matches" can do much more than the simple text matching demonstrated above.

We can also see how many times something matches:

number of times (text) matches the text (text) ... number

This produces the number of times the second text occurs within the first. The matches are not allowed to overlap. Example:

number of times "pell-mell sally" matches the text "ll" = 3
number of times "xyzzy" matches the text "Z" = 0
number of times "xyzzy" matches the text "Z", case insensitively = 2
number of times "aaaaaaaa" matches the text "aaaa" = 2

There's no "number of times WHATEVER exactly matches the text FIND" phrase since this is by definition going to have to be 0 or 1.


arrow-up.png Start of Chapter 20: Advanced Text
arrow-left.png Back to §20.4. Upper and lower case letters
arrow-right.png Onward to §20.6. Regular expression matching