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:
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.
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.
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.
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.