Do you perhaps mean to get the path-component, just after the --what you call-- key? It just happens to be the last and next-to-last component in your examples, but that is just sheer co-incidence.
The following code will extract the next path-element after the key:
Output:use strict; use warnings; use 5.012; my $key= quotemeta 'C:\abc\dfg'; # we must escape all meta-characters while (<DATA>) { chomp; if (/$key\\([^\\]+)/) { say "$_ : $1"; } else { say "$_ : no match"; } } __DATA__ C:\abc\dfg\axy C:\abc\dfg\#@$#@$@\hwllo C:\abc\dfg
C:\abc\dfg\axy : axy C:\abc\dfg\#@$#@$@\hwllo : #@$#@$@ C:\abc\dfg : no match
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
|
|---|