开发者

perl regex help matching any characters except trailing space

looking for some perl help. I'm not good with regexes. But here's basically what I need h开发者_如何学编程elp with:

-strip out the leading blank line
-regex for any value after the directory `/foo/bar/set`, excluding trailing spaces

Expected output:

55
proxy
test.event.done

Test Input file:

<leading blank :line here>     
/foo/bar/set/55
/foo/bar/set/proxy
/foo/bar/set/test.event.done

Code:

while(my $line=<>) {
    chomp($line);
    if ($line =~ m#foo/bar/set/(not sure what to match here) {
        print "$line\n";
    }
}


If the input is a directory path and if you need to extract the filename, you can use the basename methods of the Perl File::Basename module.

use File::Basename;
$filename = basename ($dirpath);


while (<>) {        
    if (m|^/foo/bar/set/(\S+)|) {
        print "$1\n";
    }       
}


(.*) matches everything in the line and you can get the value from $1.

use this to test your regex: http://regexpal.com/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜