Prev: FAQ 4.20 How do I unescape a string?
Next: very important web site to post you know your political protests....
From: George on 5 Feb 2010 21:56 Hi I am trying to get directory name from say a path c:/app/organizer what I want to get is organizer , so basically if I search from end of string and then lookbehind ($name)=($path=~(/(?<=\/)(.*?)(.*?)\z/)); but it does not result in getting to oraginzer , it fetches app/ organizer , kind of greedy I know I can do it using FILE::BASENAME , but want to understand how to do it using regexp and look back thaks /g
From: Tad McClellan on 5 Feb 2010 23:08 George <iamhello99(a)gmail.com> wrote: > I am trying to get directory name from say a path > c:/app/organizer > what I want to get is organizer , (my $name = $path) =~ s!.*/!!; or my($name) = $path =~ m!([^/]+)$!; > so basically if I search from end > of string and then lookbehind > ($name)=($path=~(/(?<=\/)(.*?)(.*?)\z/)); > > but it does not result in getting to oraginzer , it fetches app/ > organizer , No it doesn't. > I know I can do it using FILE::BASENAME , but want to understand how > to do it using regexp and look back I don't see that lookbehind is necessary. -- Tad McClellan email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
From: Xho Jingleheimerschmidt on 5 Feb 2010 23:58 George wrote: > Hi > I am trying to get directory name from say a path > c:/app/organizer > what I want to get is organizer , so basically if I search from end > of string and then lookbehind > ($name)=($path=~(/(?<=\/)(.*?)(.*?)\z/)); How would the zero-width look behind change anything? If the slash itself isn't captured in $1 or $2, who cares if it is in $& ? > > but it does not result in getting to oraginzer , it fetches app/ > organizer , kind of greedy In my hands, $name ends up as the empty string. What you indicate ends up in $2, not in $1 or $name. > I know I can do it using FILE::BASENAME , but want to understand how > to do it using regexp and look back > thaks > /g /([^\/]*)\z/ No look behind needed. Xho
From: C.DeRykus on 6 Feb 2010 08:09 On Feb 5, 6:56 pm, George <iamhell...(a)gmail.com> wrote: > Hi > I am trying to get directory name from say a path > c:/app/organizer > what I want to get is organizer , so basically if I search from end > of string and then lookbehind > ($name)=($path=~(/(?<=\/)(.*?)(.*?)\z/)); > > but it does not result in getting to oraginzer , it fetches app/ > organizer , kind of greedy > > I know I can do it using FILE::BASENAME , but want to understand how > to do it using regexp and look back Here's a possible lookbehind solution: ( $name ) = $path =~ m{ (?<=/) ([^/]*) \z }x; or, with 5.10 you could use \K (perldoc perlre): ( $name ) = $path =~ m{ / \K ([^/]*) \z }x; But, the above are somewhat obfuscated because there's a straightforward, simpler alternative: ( $name ) = $path =~ m{ / ([^/]*) \z }x The only difference is that $& now includes '/'; whereas, the latter doesn't. -- Charles DeRykus
From: Dr.Ruud on 6 Feb 2010 08:56
C.DeRykus wrote: > ( $name ) = $path =~ m{ / ([^/]*) \z }x That $name will end up false if there is no slash in $path. perl -Mstrict -le ' for my $path (undef, "", "test", "test/123") { my ($name) = $path =~ m~([^/]+)\z~; print defined($name) ? "<$name>" : "undef"; } ' undef undef <test> <123> But you should just use File::Basename for this. (beware, the module name was grossly misspelled by OP) -- Ruud |