LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 02-11-2010, 07:12 AM   #1
fukawi2
Member
 
Registered: Oct 2006
Location: Melbourne, Australia
Distribution: ArchLinux, ArchServer, Fedora, CentOS
Posts: 448

Rep: Reputation: 34
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 07:16 AM.
 
Old 02-11-2010, 08:08 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,368
Blog Entries: 52

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
You can bracket regular expressions, eg
Code:
/^upd(a(t(e)?)?)?/
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 08:11 AM.
 
Old 02-11-2010, 08:14 AM   #3
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
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";
}
 
Old 02-11-2010, 09:11 AM   #4
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
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.
 
Old 02-11-2010, 11:36 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
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 11:58 AM.
 
Old 02-11-2010, 11:45 AM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
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.
 
Old 02-11-2010, 06:32 PM   #7
fukawi2
Member
 
Registered: Oct 2006
Location: Melbourne, Australia
Distribution: ArchLinux, ArchServer, Fedora, CentOS
Posts: 448

Original Poster
Rep: Reputation: 34
Quote:
Originally Posted by neonsignal View Post
You can bracket regular expressions, eg
Code:
/^upd(a(t(e)?)?)?/
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reg expr: match a string A-Z, a-z, * or a blank ( but string can not be all blanks) matt007 Programming 4 12-22-2009 09:55 AM
What should some Regex match in awk? sebelk Programming 7 11-20-2009 07:38 PM
Help with regex to match an IP abefroman Linux - Server 5 11-02-2009 04:07 PM
Extract everything before a regex match onesikgypo Programming 5 10-21-2009 05:49 AM
How to find a match string in conf file Ameii83 Linux - Newbie 6 08-12-2008 08:42 PM


All times are GMT -5. The time now is 10:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration