Ex 421 - Mr. Burns' repast

Would someone please explain “\b\w” and “.*(?=\b\w…)”?

I’m looking at Example 421 (“Mr. Burns’ repast”).


After reading a command:
	if the unknown fish is visible and player's command matches the regular expression "\b\w+fish":
		let N be "[the player's command]";
		replace the regular expression ".*(?=\b\w+fish)" in N with "";
		now N is "[N](?)";
		now the supposed name of the unknown fish is N;
		respond with doubt;
		reject the player's command;

I understand what the code is trying to do. The example code compiles and tests as expected. I think the code is saying

If the unknown fish is visible and the player says something that includes "[some kinda]+fish":
let N be "[whatever the player said"];
remove "[everything preceding the adjective before fish]";
now add a question mark inside parentheses to the "whateverkindafish";
  • I don’t understand how to use “\b\w” and “.*(?=\b\w…)”.
  • Does reject the player’s command avert a parser error message?
  • I also do not understand “let”. In an unrelated effort, I got a “can’t find the verb” error when I copypasted this line from Chapter 21.1 Lists and entries:
let L be a list of numbers.
1 Like

There’s an overview of regular expression syntax in manual chapter 20.9. Regular expressions are a big topic (and existed long before Inform 7); the manual doesn’t try to explain them fully. I’m not sure where you’d go to really dig into the subject.

Yes. It stops the parser from running at all.

“Let” creates a temporary variable. You can only use it inside a rule or function, not at the top level. See chapter 11.15.

3 Likes

\b means ‘the start of a word’, basically.
\w+ means any number of characters that aren’t spaces.

\b\w+fish

means “any single word that ends in fish”. So “blowfish” or “uglyfish” but not “stinky fish” or “dumb ugly fish”.

.* means ‘literally anything’. And .*(?=...) means ‘literally everything that comes before the … but not including the … itself’.

So….*(?=\b\w+fish) means ‘every single thing in the player’s command that comes before a word ending in fish’. Since we replace it with nothing “”, it just deletes all words that come before a fish word.

If you try the game, it means that if you type ‘kiss the uglyfish’, then try looking, its now called “uglyfish(?)”. But it’s not perfect, because it keeps any words after, so “look at weirdfish blankly” calls it “weirdfish blankly(?)”. So to be a perfect example they’d need to replace everything after the fish command as well.

3 Likes

Oh, my heavens. I hope I’ll be forgiven for not writing this level of puzzle for my first game.

I found a typo in the official documentation and @Zed 's version:

From 20.6 Regular expression matching

(Not “asal”, because it gets the makes the leftmost match it can.)

3 Likes

Most Inform authors will never need the regex features, so don’t worry if they’re intimidating! They allow for some cool tricks—they’re how Counterfeit Monkey lets you say EXAMINE Z-REMOVER to set the remover to Z—but for most purposes the normal parser is entirely sufficient.

2 Likes

As much as I loved Counterfeit Monkey, I’m going to start with something more straightforward/with fewer player options to plan for.

2 Likes

well-spotted. It’ll be fixed in the next release. :grinning:

1 Like