in reply to Re: matching a regular expression
in thread matching a regular expression

Note that if you need to disambiguate between two strings that match for the first few characters you can require some number of characters thus:

Or else one may proceed like thus:

my @matching=grep { 0 == index $_, $userinput } @commands;

Then take actions according to whether @matching==0, @matching==1 or @matching>=2. In the latter case depending on what the commands are actually supposed to do I would either emit a warning and decide upon priority (which may be given by the ordering of @commands itself) or ask the users to refine their input.

Update: minimal example follows.

#!/usr/bin/perl use strict; use warnings; my @commands=qw/foo bar baz/; print "Choose command [@commands]\n"; { chomp(my $cmd=<STDIN>); my @ok=grep { 0 == index $_, $cmd } @commands; print "You chose [@ok]\n" and last if @ok==1; warn +(@ok>1 ? "Ambiguos command [@ok]" : "Invalid command"), ", retry!\n"; redo; } __END__

Usage example:

$ ./foo.pl Choose command [foo bar baz] fred Invalid command, retry! fo You chose [foo] $ ./foo.pl Choose command [foo bar baz] ba Ambiguos command [bar baz], retry! bar You chose [bar]