On Wed, 2002-05-01 at 18:52, garrett esperum wrote: > Hi all, > > I am using Solaris, Perl 5. > > I am having some problems manipulating strings. > > The string I am manipulating is: > > /export/home/user/www/doc-root/dir/projects/19463/1_0001.doc > > I am trying to take out the "19463" part from the string and insert it into > a variable. > > Example of my code to do this: > > $projectId = substr($projectDocDir, length($dir), index($projectDocDir, "/", > length($dir))); > > The $dir variable contains the path > "/export/home/user/www/doc-root/dir/projects". > > When I run my script, the string "1946" is returned, the last "3" is not. > What am I doing wrong?? > > Thanks you for your help!! > > -garrett
Hows about using a xeger (backwards regex)? <example> #!/usr/bin/perl -w use strict; my $filename = "/export/home/user/www/doc-root/dir/projects/19463/1_0001.doc"; my $projectid; $projectid = reverse $1 if (reverse $filename) =~ m#^.*?/(.*?)/#; print "$projectid\n"; </example> Or maybe a normal regex using the 'projects' bit of the string <example> #!/usr/bin/perl -w use strict; my $filename = "/export/home/user/www/doc-root/dir/projects/19463/1_0001.doc"; my $projectid; $projectid = $1 if $filename =~ m#/projects/(.*?)/#; print "$projectid\n"; </example> or maybe use split like this: <example> #!/usr/bin/perl -w use strict; my $filename = "/export/home/user/www/doc-root/dir/projects/19463/1_0001.doc"; my $projectid = (split '/', $filename)[-2]; print "$projectid\n"; </example> -- Today is Sweetmorn the 48th day of Discord in the YOLD 3168 P'tang! Missile Address: 33:48:3.521N 84:23:34.786W -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]