Results 1 to 2 of 2

Thread: Help in Regexp

  1. #1
    Join Date
    Feb 2004
    Location
    Eternity
    Posts
    31

    Unanswered: Help in Regexp

    Can some one explain what the following regexp mean
    1) ~ s/\/([^\/]*)$/:\1/
    2) ~ s/(.*)\/([^\/]*)$/\1\/\2/

    Thanx in Advance
    --Vijay
    Obviousness is the enemy of Correctness
    --Bertand Russel

  2. #2
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    The first regexp replaces the last occurence of '/' with ':' and the second one does nothing.

    Try this code and you will see what the regexp does:
    Code:
    #! /usr/bin/perl
    
    use strict;
    use warnings;
    
    my $path = '/path/to/my/file.txt/';
    
    (my $var = $path) =~ s/\/([^\/]*)$/:$1/;
    (my $v   = $path) =~ s/(.*)\/([^\/]*)$/$1\/$2/;
    
    print $var,"\n",$v,"\n";
    The \1 is replaced by $1. \1 is deprecated. These to expression mean Backtracking. For further details see perldoc perlre
    board.perl-community.de - The German Perl-Community

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •