begood321 has asked for the wisdom of the Perl Monks concerning the following question:

use strict; use warnings; my $folder_path = "/aboutiso/corp_gov/bod/agendas/2010"; $folder_path =~ s#/\w+/[^/]*.\w+$##; print "folderpath: $folder_path\n";[/

gives me incorrect output: /aboutiso/corp_gov

I'm trying to print /aboutiso/corp_gov/bod/agendas

Replies are listed 'Best First'.
Re: Regex output incorrect
by AnomalousMonk (Archbishop) on Dec 14, 2010 at 02:37 UTC

    What you have after your update (Note: Please mark your OP as Updated) correctly matches (and replaces with the empty string) as follows:
        /  \w+  /   [^/]*   .  \w+        $
        /  bod  /  agendas  /  2010 <end-of-string>

    You may want something like:

    >perl -wMstrict -le "my $folder_path = '/aboutiso/corp_gov/bod/agendas/2010'; $folder_path =~ s{ / [^/]+ \z }{}xms; print qq{folderpath: '$folder_path'}; " folderpath: '/aboutiso/corp_gov/bod/agendas'

      Thanks AMonk, your solution works. I have to study your code to understand solution.

      .
Re: Regex output incorrect
by ikegami (Patriarch) on Dec 14, 2010 at 02:12 UTC
    Original OP
    use strict; use warnings; my $folder_path = "/aboutiso/corp_gov/bod/agendas/2010/"; my $try = "index.html"; my $test = "$folder_path/$try"; $folder_path =~ s#/\w+/[^/]*.\w+$##; print "folderpath: $folder_path\n";

    gives me incorrect output: /aboutiso/corp_gov

    I'm trying to print /aboutiso/corp_gov/bod/agendas

    No, it doesn't print that.

    Based on what you posted, you want to remove the trailing "/2010/", so

    $folder_path =~ s{/[^/]*/\z}{};
Re: Regex output incorrect
by GrandFather (Saint) on Dec 14, 2010 at 02:08 UTC

    Running your sample code I get:

    folderpath: /aboutiso/corp_gov/bod/agendas/2010/

    Your regex doesn't match the $folder_path string at all because the regex requires a \w character at the end but the $folder_path string ends with a /. No match so no replacement so the string remains unchanged.

    True laziness is hard work

      Hi GrandFather,

      I had a typo which I corrected actually no slash is at the end of folder_path. Correct code is below, still producing incorrect output

      my $folder_path = "/aboutiso/corp_gov/bod/agendas/2010"; my $try = "index.html"; my $test = "$folder_path/$try"; $folder_path =~ s#/\w+/[^/]*.\w+$##; print "folderpath: $folder_path\n";
Re: Regex output incorrect
by wagnerc (Sexton) on Dec 14, 2010 at 02:18 UTC
    my $folder_path = "/aboutiso/corp_gov/bod/agendas/2010/"; my $try = "index.html"; my $test = "$folder_path/$try"; $folder_path =~ s#/\w+/[^/]*.\w+$##; print "folderpath: $folder_path\n"; ^D folderpath: /aboutiso/corp_gov/bod/agendas/2010/

    If u are trying to knock off the final dir in folder_path then ur regex makes no sense. It looks like u want to knock off the file name and one dir from $test.

    my $folder_path = "/aboutiso/corp_gov/bod/agendas/2010/"; my $try = "index.html"; my $test = "$folder_path/$try"; print "folderpath: $test\n"; $test =~ s{/+\w+/+\w+\.\w+$}{}; print "folderpath: $test\n"; ^D folderpath: /aboutiso/corp_gov/bod/agendas/2010//index.html folderpath: /aboutiso/corp_gov/bod/agendas

      Hi Wagner, you're right

      Here is correct code, I just need to get correct output.

      my $folder_path = "/aboutiso/corp_gov/bod/agendas/2010"; $folder_path =~ s#/\w+/[^/]*.\w+$##; print "folderpath: $folder_path\n";
Re: Regex output incorrect
by cdarke (Prior) on Dec 14, 2010 at 14:16 UTC
    You could sweat over this regular expression, or, you could use File::Basename::dirname which is part of the base (you don't have to download it).
    use strict; use warnings; use File::Basename; my $folder_path = '/aboutiso/corp_gov/bod/agendas/2010'; $folder_path = dirname($folder_path); print "folderpath: $folder_path\n";
    Gives:
    folderpath: /aboutiso/corp_gov/bod/agendas

      Yup; a nice wheel, already invented.

Re: Regex output incorrect
by jakeease (Friar) on Dec 14, 2010 at 10:41 UTC

    In your regex, the the dot matches any character, and in particular, it matches the slash before 2010. That's the first character that you don't want, so one way to get the correct output is to capture everything up to there:

    use strict; use warnings; my $folder_path = "/aboutiso/corp_gov/bod/agendas/2010"; $folder_path =~ s#((/\w+)+)/\w+$#$1#; print "folderpath: $folder_path\n";
    I replaced the dot with a slash since that's the first character I don't want.