For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2007 > scape . character









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author scape . character
Tatiana Lloret Iglesias

2007-04-27, 6:58 pm

Hi all!

how can I create a regular expression to find a software version pattern in
a file (e.g. 1.2.0) and return the last number , i.e. 0

Thanks!
T

Jeff Pang

2007-04-27, 6:58 pm

2007/4/27, Tatiana Lloret Iglesias <tlloreti@gmail.com>:
> Hi all!
>
> how can I create a regular expression to find a software version pattern in
> a file (e.g. 1.2.0) and return the last number , i.e. 0


Hi,

What's the form of your version string?
Given the case of $version_str = '1.2.0',you may write:

my ($lastnum) = $verison_str =~ /.*\.(\d+)/;

Good luck.

--
Chinese Practical Mod_perl book online
http://home.arcor.de/jeffpang/mod_perl/
Tatiana Lloret Iglesias

2007-04-27, 6:58 pm

thanks a lot!!

And how can I locate the version String it self in the file?

bla bla bla
bla bla bla 1.2.0 bla bla
bla bla bla

my pattern is number.number.number

Thanks!
T


On 4/27/07, Jeff Pang <pangj@earthlink.net> wrote:
>
> 2007/4/27, Tatiana Lloret Iglesias <tlloreti@gmail.com>:
> in
>
> Hi,
>
> What's the form of your version string?
> Given the case of $version_str = '1.2.0',you may write:
>
> my ($lastnum) = $verison_str =~ /.*\.(\d+)/;
>
> Good luck.
>
> --
> Chinese Practical Mod_perl book online
> http://home.arcor.de/jeffpang/mod_perl/
>


Jeff Pang

2007-04-27, 6:58 pm

open FILE,$file or die $!;
while (<FILE> ) {
next unless /\s+(\d+\.\d+\.\d+)\s+/;
my $version_str = $1;
my ($lastnum) = $verison_str =~ /.*\.(\d+)/;
print $lastnum,"\n";
}
close FILE;

(Note for no test.)

2007/4/27, Tatiana Lloret Iglesias <tlloreti@gmail.com>:
> thanks a lot!!
>
> And how can I locate the version String it self in the file?
>
> bla bla bla
> bla bla bla 1.2.0 bla bla
> bla bla bla
>
> my pattern is number.number.number
>
> Thanks!
> T
>
>
> On 4/27/07, Jeff Pang <pangj@earthlink.net> wrote:
>



--
Chinese Practical Mod_perl book online
http://home.arcor.de/jeffpang/mod_perl/
Chas Owens

2007-04-27, 6:58 pm

On 4/27/07, Tatiana Lloret Iglesias <tlloreti@gmail.com> wrote:
> thanks a lot!!
>
> And how can I locate the version String it self in the file?
>
> bla bla bla
> bla bla bla 1.2.0 bla bla
> bla bla bla
>
> my pattern is number.number.number

snip

Loop over the lines of the file applying the regex as you go:

#!/usr/bin/perl

while (<> ) {
if (/(\d+\.\d+\.\d+)/) {
print "found version $1 on line $. of file $ARGV\n";
last;
}
}

Which is run like this:
../find_ver.pl file_to_find_version_in

or the one liner:

perl -lne 'print "found version $1 on line $. of file $ARGV", last if
/(\d+\.\d+\.\d+)/' file_to_find_version_in
Chas Owens

2007-04-27, 6:58 pm

On 4/27/07, Jeff Pang <pangj@earthlink.net> wrote:
> open FILE,$file or die $!;
> while (<FILE> ) {
> next unless /\s+(\d+\.\d+\.\d+)\s+/;
> my $version_str = $1;
> my ($lastnum) = $verison_str =~ /.*\.(\d+)/;
> print $lastnum,"\n";
> }
> close FILE;

snip

umm, why are you testing twice? Just capture the last digit (instead
of the whole version number) in the first regex.

next unless /\s+\d+\.\d+\.(\d+)\s+/;
print "$1\n"
Jeff Pang

2007-04-27, 6:58 pm

> umm, why are you testing twice? Just capture the last digit (instead
> of the whole version number) in the first regex.
>
> next unless /\s+\d+\.\d+\.(\d+)\s+/;
> print "$1\n"
>


b/c he asked two questions,
1) how to capture version string;
2) how to capture last number from version string.

surely I know testing one time is enough.

--
Chinese Practical Mod_perl book online
http://home.arcor.de/jeffpang/mod_perl/
Martin Barth

2007-04-27, 6:58 pm

Hi,

is your version allways number dot number dot number? or can it be..
e.g. 1.2.1a or 1.6
Chas Owens

2007-04-27, 6:58 pm

On 4/27/07, Jeff Pang <pangj@earthlink.net> wrote:
>
> b/c he asked two questions,
> 1) how to capture version string;
> 2) how to capture last number from version string.
>
> surely I know testing one time is enough.


Sorry, it just looked odd that you were making one capture and then
doing nothing with it but doing another capture.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com