Checking unknown player sentence input

So I am trying to expand my knowledge on regexp's by programming a script that takes a sentence that a player types, and then gives a response based on what words makes up that sentence. i.e:

Spoiler
player input: How are you doing? response: I'm doing fine.
player input: How am I doing? response: You are doing great!
player input: Who are you? response: I am a computer!
player input: Who am I? response: You are the person I am talking too.
I know how to get the player's input however I am having trouble with the match data.

For example:

/([^\s]\w*)/ #this will return all words in the player input however it will only use the match #variable $1. To semi solve that problem I used the following gsub.input.scan(/([^\s]\w*)/).each {|word| #Extra code here}Now the above regexp works (I have tested it on very long sentences and it will pick up each word) but when going through the words that were matched it will forget the last one that was matched. This is undesirable as it will make matching the words to get proper responses all that much more difficult.

So my question is there a more efficient regexp that will go through the players input?

And on another note what would be the most efficient method for checking the data? I was thinking case when statements but with the amount of data I could see where that could become very messy.
What do you mean by "it will forget the last one it matched"?
Because there is only one set of parentheses there is technically only one match made at a time (from the way I understand it) this match gets stored in the variable $1 (if there were two sets then there would be $1 and $2 available). When it gets to the next match it will save it to that variable thus losing the word that was last saved to it.

Hopefully I make more sense I still have a lot to learn with regexp so I may have trouble properly describing it.
But if you're looping through the input, you should only need to look at each word once anyway. That said, I notice you're using gsub, which I don't think is going to do what you intended.
My logic for the extra word checking was from using nested case when statements. The first case would succeed but the second wouldn't because the first match was no longer there. Also the reason for me asking if there is a better logic format for checking the matches. < (which I now no longer need found out that nesting them was one of my problems)

Don't know why I was using gsub (I know it is for replacing parts of a string vs what I was using it for) been working to many late nights I guess, believe it should have been set to scan.

Actually based on the thoughts you have given me I was able to figure this out a whole lot more!
Have you totally cracked it or do you still need help with anything?
There are a few things I need to work out in it though I will probably do it tomorrow.

One of them is I want to put some of the more general words into arrays then if any in that array match a word in the player input to give a response. So for instance putting all the English words of greeting into an array (so something like this).

Spoiler
module Lang GREETING = [] GREETING[0] = "hello" GREETING[1] = "hi"end#inside the block for checking player inputcase word when GREETING[0..1] # this part I would have match the word to what is stored in the # greeting array I can do it with just one array element but I need # to work out a way to check through all of them which I will do # tomorrow. #do stuffend
Reason for doing like this is it would help keep the when statements easier to read. Any thoughts on this?

Edit: I have the right idea but with the incorrect way to do it.

You've helped me immensely though so thank you. 🙂
Couldn't you just split the string at /\s+/ (one or more whitespaces, tabs, newlines, ...), instead?
You don't need to specify every index for your array:

greeting = ["hello", "hi", "hey"] etc.

In your case statement, do

when greeting[greeting.index(word)]

I don't have time to test that right now but it should work.
Actually I switched over to using a hash and then used the .each {|key, value| .... } which works a whole lot more efficiently then the case when statements I was planning on using. (I can have pretty much all my checks now on just 5 lines of code using this method). Yea I know I don't have to specify array elements I just do it so I can keep track of what elements are assigned to the different values (pretty much for my own viewing purposes).

@lavra you mean when checking player input? Well the reason for using /([^\s]\w*)/ is that it removes the white space from the words then I can check the word without any modification (I do downcase all player input later for easier checks though). By testing /\s+/ it just matches the white space vs the words (not certain how I would match the words using this method). [^\s] removes white space \w* matches any word character including numbers with any length.

Edit: Unless you were referring to using the pre_match variable $`. Which I think could work.
You use the regex for the split method like this:

Code:
"abc      xyz a!".split(/\s+/)
Note that this will also match symbols like "!" and "?". You can instead split at non-word characters like this:
Code:
"abc      xyz a!".split(/\W+/)
Okay I am going to have to do some reading on using .split now. 😉

Currently my code is in complete working order but if I can make it even more efficient and learn something at the same time then that will be a big bonus.

Edit: .split was able to remove one line of redundancy from my code plus the regexp looks better. 😀

Original thread: https://forums.rpgmakerweb.com/threads/checking-unknown-player-sentence-input.41229/