Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-11-2010, 06:12 AM
|
#1
|
|
Member
Registered: Oct 2006
Location: Melbourne, Australia
Distribution: ArchLinux, ArchServer, Fedora, CentOS
Posts: 448
Rep:
|
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?/
/^upl?o?a?d?/
This works EXCEPT for treating 'upte' and 'upod' as matches.
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? 
Last edited by fukawi2; 02-11-2010 at 06:16 AM.
|
|
|
|
02-11-2010, 07:08 AM
|
#2
|
|
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Squeeze (Fluxbox WM)
Posts: 1,357
|
You can bracket regular expressions, eg
Or you could just match on the first three characters and then do a second check that what they entered matches the start of the full command string.
Last edited by neonsignal; 02-11-2010 at 07:11 AM.
|
|
|
|
02-11-2010, 07:14 AM
|
#3
|
|
Member
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215
Rep:
|
I think this will work fine for you.
Code:
$a=<STDIN>;
if($a=~/^(upda?|updat?|update?|update)/)
{
print "UPDATE\n";
}
elsif($a=~/^(uplo?|uploa?|updload?|upload)/)
{
print "UPLOAD\n";
}
else
{
print "NONE\n";
}
|
|
|
|
02-11-2010, 08:11 AM
|
#4
|
|
Member
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 439
Rep:
|
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.
|
|
|
|
02-11-2010, 10:36 AM
|
#5
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
You could use the patterns in case statements instead of a string of if/then/else statements.
Last edited by jschiwal; 02-11-2010 at 10:58 AM.
|
|
|
|
02-11-2010, 10:45 AM
|
#6
|
|
Senior Member
Registered: Jul 2005
Distribution: Slackware
Posts: 2,006
Rep: 
|
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.
|
|
|
|
02-11-2010, 05:32 PM
|
#7
|
|
Member
Registered: Oct 2006
Location: Melbourne, Australia
Distribution: ArchLinux, ArchServer, Fedora, CentOS
Posts: 448
Original Poster
Rep:
|
Quote:
Originally Posted by neonsignal
You can bracket regular expressions, eg
|
That was my other thought, but it didn't seem 'graceful' enough, lol
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 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:09 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|