Welcome to the most active Linux Forum on the web.
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 09-03-2008, 03:46 PM   #1
LQ Newbie
Registered: Jun 2006
Posts: 13

Rep: Reputation: 0
Trying to combine lookahead and variable substitution

I've tried this in a few different tools, but I think perl is what I want to use. I'm not too familiar with it so be gentle.

I'm trying to strip off characters from a single line of text. I want to end up with only the version of a package name from this:

"recommended : bzip2-program-1.0.3"

So I figured I would do a non-greedy search for a dash, then one or more digits since it could very well be:

"recommended : bzip2-program-10.0.3"

Then, look ahead to find the period, then use a place holder to hold the version up to another dash in case I have something like this:

"recommended : bzip2-program-1.0.3-12"

I want to end up with: "1.0.3"

Any suggestions would be much appreciated. If you have a suggestion to use another tool, I would love to hear it as well.
Old 09-03-2008, 04:32 PM   #2
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
You could use a regular expression with perl
I see this as:
^.+- : match one or more caracter until the dash (included)
(\d+\.?\d+\.?\d+): match one digit or more followed by a dot or not, etc...
-? : match one dash or not
\d* : match zero or more digit

Note that it will remove the "-12" from "1.0.3-12", it is what you want if I understand correctly, no?

You use it like:
my $str;
$str = "recommended : bzip2-program-1.0.3-12";
$str =~ s/^.+-(\d+\.?\d+\.?\d+)-?\d*/\1/;
print $str . "\n";

$str = "recommended : bzip2-program-1.0.3";
$str =~ s/^.+-(\d+\.?\d+\.?\d+)-?\d*/\1/;
print $str . "\n";

Last edited by keefaz; 09-03-2008 at 04:35 PM.


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
variable substitution in sed gaynut Programming 1 07-14-2008 07:38 AM
substitution.... kadhan Programming 12 01-28-2008 10:09 PM
Bash variable substitution daYz Programming 3 04-14-2006 01:16 PM
Variable substitution (stupid noob) question in tcsh King of Men Linux - Newbie 4 12-04-2005 09:12 AM
shell question variable substitution dpressm Programming 9 11-18-2004 07:00 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:29 AM.

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