You are not logged in.
Pages: 1
$element = "-rw-rw-rw- 1 asdfs itsupp 4 Apr 14 23:51 /tmp/xyz.out";
I've got many strings, similar to the above, and I just need to extract the '/tmp/xyz.out' part into a new variable. I know this would be easy to do by just by calling cut via a system command, but I would prefer to do it completely in perl and I guess using regex, which I'm sure is pretty easy too, but I don't know how. Can anyone help?
Cheers,
sw
Last edited by sw (2009-05-11 09:19:08)
Offline
if ($element =~ /(\S+)$/)
{
my $path = $1;
}
The regex will grab all non-whitespace characters at the end of $element. If there's a chance that there could be trailing whitespace, use "/(\S+)\s*$/" instead.
More info on perl regexes:
http://perldoc.perl.org/perlretut.html
http://perldoc.perl.org/perlre.html
Last edited by Xyne (2009-05-11 09:30:24)
My Arch Linux Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
Thanks for you answer, it works perfectly. I have been trying to form the answer myself, but couldn't quite get my head around it, now you have shown me that it makes complete sense. Thanks for the links too!
sw
Offline
You could also use split() for this, it might be a little faster than a regex.
[git] | [AURpkgs] | [arch-games]
Offline
Pages: 1