jeudi 13 février 2020

Best Way to Create a Class That Returns Commands on Regex Matches

I have a bot that responds to user input, but before selecting what to do, it needs to run the string through Regex and select the appropriate command based on whether a match is successful. For example,

if (Regex.Match(...,.....).Success)
{
// Do #1
}
else if (Regex.Match(...,.....).Success)
{
// Do #2
}

This is not scalable or maintainable anywhere above 5 potential regex matches. What's the best way to design a class that can step in to solve this for me?

Maybe a class called CommandDictionary that scans the strings for a match, generates a list of commands that corresponds to each successful regex match, and then picks the command that is associated with the highest priority? For example, if "Hello" matches "Greet" and "Ignore", but "Greet" has higher priority, the response will be Do Greet.

Thanks

Aucun commentaire:

Enregistrer un commentaire