#1
  1. perl virgin
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Seattle, WA
    Posts
    460
    Rep Power
    19

    using regex to get last element of string.


    so i have a sort of directory listing line like this.

    Code:
    drwxr-x---   - joe stream          0 2014-04-03 05:31 hdfs://joseph.foo.com:8080/projects/anchor/Signal/prod/Daily/20140502
    how do i get the last element with regex..i know i can do it via split..but i was just curious if i were to go regex, what would it be. seems simple but somehow the regex escapes me...

    thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,233
    Rep Power
    1298
    Code:
    my $str = 'drwxr-x---   - joe stream          0 2014-04-03 05:31 hdfs://joseph.foo.com:8080/projects/anchor/Signal/prod/Daily/20140502';
    
    my ($path) = $str =~ /(\S+)$/;
    
    print $path;
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,233
    Rep Power
    1298
    Code:
    my $str = 'drwxr-x---   - joe stream          0 2014-04-03 05:31 hdfs://joseph.foo.com:8080/projects/anchor/Signal/prod/Daily/20140502';
    
    my ($path) = $str =~ /(\S+)$/;
    
    print $path;

    Comments on this post

    • Laurent_R agrees
  6. #4
  7. perl virgin
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Seattle, WA
    Posts
    460
    Rep Power
    19
    Originally Posted by FishMonger
    Code:
    my $str = 'drwxr-x---   - joe stream          0 2014-04-03 05:31 hdfs://joseph.foo.com:8080/projects/anchor/Signal/prod/Daily/20140502';
    
    my ($path) = $str =~ /(\S+)$/;
    
    print $path;
    well that was easy..didnt think about negation..thanks

IMN logo majestic logo threadwatch logo seochat tools logo