Tweaking/Substituting the text of command on the fly

I’m using bog-standard footnotes straight out of recipe/sample 300.

That’s all working fine. A player can look up a note from a source with the language “[source] [number]”, so looking at note 1 from source cas would require player input “cas 1”.

Well, testers have asked for a way to also input it without the space, as in “cas1”.

After a day or so, I’ve determined that I can’t do it myself. Using the documentation, I can break up words to fix commands (“getall” to “get all”). I’m getting the number stuff wrong.
understand "cas[number]" as looking up a cas is no good.

Lots of things seem to work fine without the number. [number] seems to be the source of my problem. stuff like:
if the player's command matches "cas[number], try looking up a cas [number] and the like don’t work.
if the player's command matches "cas[number], replace the matched text with "cas [number]"
This does something:

After reading a command:
	if the player's command includes "[number]":
		say "you entered a number.";
		reject the player's command.

but this doesn’t:

After reading a command:
	if the player's command includes "[number]":
		say "you entered the number [number].";
		reject the player's command.

Explicitly changing the command to a defined text, as in

After reading a command:
   let PC be "[the player's command]";

etc.
It seems to me that the big problem I’m having is getting a number (that might be treated as text) to behave like a number. The lack of spacing with a number seems to be part of the problem, too. phrases like “cas1” get dumped to parser errors (That's not a verb I recognize) without trying to substitute/match parts of the command.

I see that documentation discusses regular expressions with regard to substitutions, but honestly I have no idea what I’m looking at.

How can I change the command “cas1” into “cas 1”? I feel I’m missing something obvious.

Players. Whuddyagonnado.

After reading a command:
  if the player's command matches the regular expression "^(cas|xyz)(\d+)",
    change the text of the player's command to "[text matching subexpression 1] [text matching subexpression 2]";

I did the (cas|xyz) thing just in case there were terms other than cas you might like to do something similar with; expand at will, e.g., (cas|xyz|abc)

3 Likes

Perfect. Thanks a lot!

If you want to go down this route, it’s the number understood.

After reading a command:
	if the player's command includes "[number]":
		say "you entered the number [the number understood].";
		reject the player's command.

However, the includes "[number]" test looks for a token. It will catch 3 but not 3x. So this is not helpful for your example, but in case it turns up in the future.

Ha, I like a good shortcut myself!

Thanks for this, I’m sure it will

EDIT:
One last thing, if you stumble across this thread and are as clueless about regular expressions as I am, you might wonder how to accept commands that aren’t case sensitive. That is, you might want Cas1, CAs1, cas1, CAS1, and so forth to all work the same. The matchable text must be flagged as case insensitive with (?i). Here it is, added to Zed’s magic invocation:

After reading a command:
  if the player's command matches the regular expression "^(?i)(cas|ps|ahm|dsc)(\d+)",
	change the text of the player's command to "[text matching subexpression 1] [text matching subexpression 2]";

One can also say matches the regular expression "^(cas|xyz)(\d+)", case insensitively.

(?i) and (?-i) can be reserved for turning insensitivity on or off within an expression (not that I can recall having ever wanted to do that across decades of text munging). (It works just fine at the front, of course, but I figure one should take what opportunities one has to make regexps more readable.)