[TriLUG] regular expression

Mike Broome mike at 1000plus.com
Thu Aug 16 13:50:48 EDT 2001


Andy,

The following worked for me on your sample input below:
    $linein =~ s|^.*/.+\.(\d+)\.\d+$|$1|;

It searches for any characters up to a '/' then any characters up to a
'.' then any digits (which are saved in $1) up to another '.' then more
digits til the end of line.  It replaces the string with the digits in
the middle that were saved.

If the string will always have the two '.' characters, another option
would be to split the string on those and just pull out the middle part:

    @middle = split('\.', $linein);
    print "$middle[1]\n";

Mike

On Thu, Aug 16, 2001 at 10:30:44AM -0700, acoliver wrote:
> Hi all,  I'm not having a PERL day.
> 
> Any one of you regular expression gurus have the answer to this
> 
> If I have this (coming line by line from stdin):
> $linein = 01072001/string.189.123123
> 
> What would the regular expression be to suck out '189'?  The directory name
> is fixed length, the string is variable and the trailing numeric is
> variable.  The only thing I know in other words is I want to match
> everything between the two dots but not including them.
> 
> -Andy

-- 
Mike Broome
mike at 1000plus.com



More information about the TriLUG mailing list