You are not logged in.

#1 2009-05-11 09:18:21

sw
Member
Registered: 2009-05-11
Posts: 6

perl regex help

$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

#2 2009-05-11 09:28:05

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: perl regex help

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 StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#3 2009-05-11 09:57:28

sw
Member
Registered: 2009-05-11
Posts: 6

Re: perl regex help

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! smile

sw

Offline

#4 2009-05-11 12:19:55

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: perl regex help

You could also use split() for this, it might be a little faster than a regex.

Offline

Board footer

Powered by FluxBB