Cutting Text From the End of a Command

My goal is to see if I can cut certain text off of what is otherwise a valid command and still have the command execute.

To have some context for what I’m doing here, consider that I have this test:

Test describe_001 with “* check summary of unvisited broad walk / look / north / * check summary of visited broad walk / south / * check summary of flower walk / east / * check empty description of palace gate / west / * check full description of palace gate / impressions” in Palace Gate.

The goal here is to be able to annotate tests with what to actually look for. So a command with an asterisk says what to check in the next command’s output.

The commands with “*” are currently handled like this:

After reading a command (this is the ignore test outcome rule):
    if the player's command matches the regular expression "^\*":
        reject the player's command.

However, what I’m curious about is if I can do something a little different. Specifically, let’s say a command is given like this:

> look * check that the description shows the visited text

So not two commands now. Just one, with the test annotation tacked on at the end.

So here I want the look command to work as normal but obviously for that to happen the * check that the description shows the visited text would have to go.

I’m not sure if there is a good way to do this. I started off with this:

After reading a command (this is the ignore test outcome rule):
    if the player's command matches the regular expression "\b\*":
        cut the matched text.

That doesn’t work at all but I wasn’t so much expecting it to. At the moment I just wanted to check: am I on the right path here?

Regular expressions are well above my pay grade, but have you looked at 20.8 in Writing with Inform, and especially the Northstar and Cave-Troll examples? It looks like “change the text of the player’s command to (whatever)” is the key instruction you’ll need, assuming you can wrangle the regex stuff to strip out the asterisk and everything after (which I think must be easy, it’s just that every time I start looking at regex commands my brain starts leaking out of my ears).

I think you can do something like this (untested):

After reading a command:
    if the player's command matches "* [text]":
        cut the matched text.

(See 18.33 in WwI)

Yep, I tried something similar but it seems not to work. More specifically, I originally was doing this:

 Understand "* [text]" as a mistake.

So I realized I could use the [text] bit. When I try a construct similar to this:

After reading a command (this is the ignore annotated test outcome rule):
	if the player's command matches "* [text]":
		cut the matched text.

You get the following error:

You wrote ‘if the player’s command matches “* [text]”’ : but the ‘[text]’ token is not allowed with ‘matches’ or in table columns, as it is just too complicated to sort out: a ‘[text]’ is supposed to extract a snippet from the player’s command, but here we already have a snippet, and don’t want to snip it further.

In playing around in Rubular, I realize that the regular expression I need is this:

\s\*(.*)

That flags the space, the asterisk, and then all of the remaining text on the line. That doesn’t translate directly to Inform 7, however. So I need to play around and figure out how Inform wants it.

This seems to work at least with some initial tests:

After reading a command (this is the ignore annotated test outcome rule):
    if the player's command matches "\s\*(\L*)":
        cut the matched text.

Although, oddly, the manual describes \L as matching any non-lowercase letters. Yet all the letters in the command are lower case. I’m also not sure why that would then also match the spaces since regular expressions are usually pretty specific on what they match against.

So I replaced it with \U just to check that out. According to the manual, that should only match upper case letters. But it works just the same. So, at a glance, it looks like the regular expression matching isn’t actually working as the manual describes although in my particular case, I do actually get what I want.

But that led me to realize that I could just do this:

"\s\*(*)"

And that, too, seems to work.

So, I have to admit, regular expressions in Inform are confusing me a little bit.

1 Like

Be wary of the difference between:

if the player's command matches...

and

if the player's command matches the regular expression...

The latter is needed to use regular expression matching.

I tend to overspecify regular expressions so that I can be sure of what I’m getting. Does this do what you want?

"Commentary"

Place is a room.

After reading a command (this is the comment without action rule):
    let user comment be text;
    if the player's command matches the regular expression "^\*\s(.*)$":
	    now user comment is text matching subexpression 1;
	    say "COMMENT ONLY: [user comment][paragraph break]";
	    reject the player's command.

After reading a command (this is the comment with action rule):
    let user comment be text;
    if the player's command matches the regular expression "^(.*)\s\*\s(.*)$":
	    now user comment is text matching subexpression 2;
	    change the text of the player's command to text matching subexpression 1;
	    say "COMMENTED ACTION: [user comment][paragraph break]".

Test me with "look / north / * check jumping / jump / wave * check waving".

Note that the user comment variable is not strictly necessary.

Ah, quite interesting! Thank you for working that up. Yes, that indeed does have the desired effect. I also appreciate you bringing up the distinction as it’s one I was entirely overlooking.

This is extremely helpful. Again, much thanks for taking the time with this.

@drpeterbatesuk suggested some useful phrases to make matching clearer.