Fun with RegEx

K.V.
03 Sept 2017, 06:32
^((see |view |access |get |)hint |hints |help |info |information |)(regarding |concerning |for |on |about | |)(?<object>.*)$

Okay...

How do i make it understand VIEW A/SOME/THE HINT/HINTS ABOUT THE THING?


K.V.
03 Sept 2017, 06:33

Aargh!

The answer hit me the moment I pushed POST!

Tested and approved:

^((see |view |access |get |)((the |a |some |)(hint |hints |help |info |information |)))(regarding |concerning |for |on |about |with | |)(?<object>.*)$

What did I leave out?

The player can enter:

  • hint thing
  • about thing
  • get hint thing
  • get some hints about the thing
  • view the hints regarding the thing

Blah, blah, you see where I'm headed...

What else (that would end in OBJECT) might the player enter?


Doctor Agon
03 Sept 2017, 07:41

tell me about thing
give me advice about thing
advise me about thing


K.V.
03 Sept 2017, 07:51

A-ha!

I appreciate you advising me to add 'advise' and 'advice', Doctor Agon!

I never considered throwing 'me' in there, either.

Well played!


Dcoder
03 Sept 2017, 11:18

If we want to get even more pedantic, how about:

hint about THE object (not just hint about object)

clue | cheat | 911 | 999 about object

teach me | educate me | inform me | send me to school about object

Then there's:

"Push the easy button"

and the perennial favorite:

"Why don't you just tell me how to kill the !@#$ing troll?"


mrangel
03 Sept 2017, 11:33

With your current pattern, it looks like this command will be triggered just by the name of the object, omitting all the words before it. Is that intentional?


K.V.
03 Sept 2017, 17:11

Whoa! Good catch, mrangel!

How about this one:

^((see |view |access |get |show |advise |)((the |a |some |me |)(hint|hints|help|info|information|advice|))) (regarding |concerning |for |on |about |with |)(?<object>.*)$

Dcoder

I've got 'the' in there in the beginning (and #object# will ignore the indefinite articles 'a,an,the' (I think; it works at any rate).).

All your suggestions are spot on, too, though!

What is 999?

Is that like 911 in the UK?

(This reminds me: I need 411 in there too!)


Why don't you just tell me how to kill the !@#$ing troll?

You can actually use the word !@#$ing in my games!

I have it set up to ignore many profane indefinite articles.

So TELL ME ABOUT THE DAMNED THING or GO F--KING NORTHWEST will actually be understood.


mrangel
03 Sept 2017, 17:21

Hmm ... now it will take " objectname" with a space before, but "advise object" needs a double space.

I'd think about something like... (off the top of my head) ^((see|view|access|get|show|advise|)\s*((the|a|some|me|)\s*(hint|hints|help|info|information|advice|)))\s*(regarding|concerning|for|on|about|with|)(?<=\S)\s+(?<object>.*)\s*$

(Assuming we're using PCRE or similar that supports (?<= properly. I haven't checked)

(You may have noticed I spend way too much time thinking about these things; it comes from too much experience developing IRC bots, and the cases where they respond oddly to things that you would never expect a real person to type. In a game, I think that someone typing a space and then an object name is clearly just probing for bugs. But I still have this urge to make my systems behave sanely in every possible circumstance)


K.V.
03 Sept 2017, 18:23

Does \s*match 0 or more instances of whitespace?

What in tarnation does (?<=\S) do? Look for 0 or 1 instances of non-whitespace?

I still have this urge to make my systems behave sanely in every possible circumstance

Me too!

...and more for me than for the player!


mrangel
03 Sept 2017, 18:30

Yep, \s* matches zero or more whitespace characters; and (?<=\S) matches the zero-width space between characters where the previous character was not whitespace (basically ensuring that if's preceded by several (x|y|z|) conditions, at least one of them matched something)


K.V.
03 Sept 2017, 18:43

> show me the hints regarding the quest
I can't see that.

> show me hints regarding the quest
Which hint would you like to view?
Hint 1


It has evolved a little more now:

^((see|view|access|get|show|advise|)\s*((the|a|some|me|)\s*(the|a|some|)\s*(hint|hints|help|info|information|advice|)))\s*(regarding|concerning|for|on|about|with|)(?<=\S)\s+(?<object>.*)\s*$


> show me the hints regarding the quest
Which hint would you like to view?
Hint 1


...but...

> see me some quest
Which hint would you like to view?
Hint 1


> me quest
Which hint would you like to view?
Hint 1


I may set up a few separate command patterns that will invoke this same script...

...or maybe I should just stick with ^(hint|hints|advice|advise|help|)\s*(about|regarding|on|)\s*(about|regarding|)(?<=\S)(?<object>.*)$ and provide a nice help menu with all of my commands listed.

If you need help during play:

  • Enter HINT (object name) for a clue about something specific.
  • Enter HINT, 911, 999, 411, CLUE, ADVICE, ADVISE, INFO, INFORMATION, ABOUT, WTH, WTF, QUIT, or UNDO1 to access the HINTS MENU.

K.V.
03 Sept 2017, 22:26

Can I separate different patterns when using RegEx patterns?

Like hint about #object#;tell me about #object#, except using | instead of ;?


Doctor Agon
03 Sept 2017, 22:34

Hi K.V. I think in this page the Pixie has put up: http://docs.textadventures.co.uk/quest/complex_commands.html
I think this is an example of just such a thing

 ^((get|pick up|take) (the |)(?<object1>.*) (using|holding|with) (the |)(?<object2>.*)|(using|holding|with) (the |)(?<object2>.*) (get|pick up|take) (the |)(?<object1>.*))$

The | would have to come between the closed then open brackets )|(


hegemonkhan
03 Sept 2017, 23:05

(filler for editing my post, getting it updated/posted)


@ KV (or who-ever is interested):

"Like hint about #object#;tell me about #object#, except using | instead of ;? (KV)"

somewhere in the internal coding (see source files) is the handling/parsing of the regex, which you'd adjust for it to use the ';' instead of the '|' (this is called 'piping/pipe' and/or 'or-ing' character/symbol)

essentially, the parsing iterates/loops/recurses through the input command (a string), checking the order/pattern of the characters/symbols using logic/conditions that produce the desired parsing of its pattern for whether it's a valid and/or the desired pattern or not

https://en.wikipedia.org/wiki/Axiom_(computer_algebra_system)
https://en.wikipedia.org/wiki/Axiomatic_semantics
https://en.wikipedia.org/wiki/Mathematical_logic

https://www.codeproject.com/Articles/6673/A-Very-Simple-Parser

etc links (search 'axioms/parsing' --- I couldn't yet find an example of an axiom parsing example... but maybe you can find and read up about it. Until then, use the math parsing example link: 'a very simple parser' above. Axioms are just like the math parsing example, except they're usually the basics/building-blocks of parsing a programming language. If I find a good link on it, I'll definately post it, but I've not found one yet, and also axioms and parsing is still beyond my ability, but I get the general idea of how they're done, but it still confuses me)


argh....

that's what I was looking for... 'grammers/grammars', I forgot the word...

(search these topics, they're all related: axioms, grammars/grammers, parsing)

http://www.cs.cmu.edu/~me/212/handouts/parsing.pdf
https://en.wikipedia.org/wiki/Parsing
https://en.wikipedia.org/wiki/Formal_grammar
https://en.wikipedia.org/wiki/Ambiguous_grammar
https://www.codeproject.com/Articles/20450/Simple-CSS-Parser


K.V.
04 Sept 2017, 01:47

Ah, sorry I wasn't clear...

I meant could I do this:

^(hint) (?<object>.*)$|^(about) (?<object>.*)$

It didn't work though. (I didn't think it would.)

After reading the last two posts, though: I learned that we can do this:

(^(hint) (?<object>.*)$|^(about) (?<object>.*)$)

Doctor Agon
04 Sept 2017, 06:46

Could you also do:

^((hint) (?<object>.*)|(about) (?<object>.*))$

K.V.
04 Sept 2017, 08:00

@ Doctor Agon

Ah, yes!

I prefer that one. (It won't be 'hint' and 'object'. I just used the simplest example I could think of. It will be all the 'complicated' stuff from the earlier post that would understand nearly anything the player might enter. I just didn't want ME OBJECT (or just OBJECT) to trigger the hint menu.)


Everyone has been super helpful, by the way! (Especially considering I'm just now learning about RegEx...)

Thanks, everyone!


The Pixie
04 Sept 2017, 08:01

somewhere in the internal coding (see source files) is the handling/parsing of the regex, which you'd adjust for it to use the ';' instead of the '|' (this is called 'piping/pipe' and/or 'or-ing' character/symbol)

It is deeper than that. Quest uses the .NET system, so you will not find it in the source code. However, that does mean there are plenty of resources on it on the web, eg:

https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference