Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 02-11-2010, 06:12 AM   #1
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.

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

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
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.
Old 02-11-2010, 07:14 AM   #3
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
I think this will work fine for you.
print "UPDATE\n";
print "UPLOAD\n";
print "NONE\n";
Old 02-11-2010, 08:11 AM   #4
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, 10:36 AM   #5
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
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.
Old 02-11-2010, 10:45 AM   #6
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, 05:32 PM   #7
Registered: Oct 2006
Location: Melbourne, Australia
Distribution: ArchLinux, ArchServer, Fedora, CentOS
Posts: 448

Original Poster
Rep: Reputation: 34
Originally Posted by neonsignal View Post
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:

Thanks for all the suggestions folks


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 08:55 AM
What should some Regex match in awk? sebelk Programming 7 11-20-2009 06:38 PM
Help with regex to match an IP abefroman Linux - Server 5 11-02-2009 03:07 PM
Extract everything before a regex match onesikgypo Programming 5 10-21-2009 04:49 AM
How to find a match string in conf file Ameii83 Linux - Newbie 6 08-12-2008 07:42 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:52 PM.

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