regex match string from start to find unique combinations
Well it's late, and I'm way too inexperienced with perl/regex to figure this out on my own...
I'm writing a perl script to accept input (commands) from the user. I want to implement a 'closest match' type scheme on accepting the input. Example: - A valid command is 'update' and 'upload' - The user should be able to type 'upd' or 'update' etc to execute the 'update' command. 'upte' is not valid. - The user should be able to type 'upl' or 'uplo' etc to execute the 'upload' command. upod is not valid. - The command 'up' can't be matched to a unique command. I'm using the following regex at the moment: Code:
/^upd?a?t?e?/ I think I need a way in the regex similar to ? except to say "match the preceding character or nothing, and stop looking" rather than "match the preceding character, or don't" Any ideas folks? :) |
You can bracket regular expressions, eg
Code:
/^upd(a(t(e)?)?)?/ |
I think this will work fine for you.
Code:
$a=<STDIN>; |
Just out of curiosity, why aren't 'upte' and 'upod' valid matches? If it's 'closest match', anything that could uniquely match would seem to be valid.
I think that I would use a soundex algorithm, and be done with it. |
You could use the patterns in case statements instead of a string of if/then/else statements.
|
I would consider approaching the problem from the other direction. If say, the user typed "up", use the regex "up.*" on each valid command. Since that regex matches more than one command, it's ambiguous (and you can create a nice error message listing out the possibilities). If the user typed "upd", then the regex "upd.*" would only match "update", so that must be the desired command.
|
Quote:
Now that I'm awake a bit better, my Googling skills are working better, and I think I've found my solution in here: http://docstore.mik.ua/orelly/perl/cookbook/ch06_21.htm http://perldoc.perl.org/Text/Abbrev.html Thanks for all the suggestions folks :) |
All times are GMT -5. The time now is 06:31 AM. |