开发者

Why doesn't this perl regex capture the last character?

开发者 https://www.devze.com 2023-02-16 19:05 出处:网络
let $PWD =/Unix_Volume/Users/a开发者_JS百科/b/c/d I would expect: echo $PWD | perl -ne \'if( /(\\w+)[^\\/]/ ){ print $1; }\'

let $PWD = /Unix_Volume/Users/a开发者_JS百科/b/c/d

I would expect:

echo $PWD | perl -ne 'if( /(\w+)[^\/]/ ){ print $1; }'

to display "Unix_Volume". However, it displays "Unix_Volum." Why doesn't the regex capture the last character?


(\w+) => Unix_Volum

[^\/] => e (not a /)

/ => /


Try:

export PWD=/Unix_Volume/Users/a/b/c/d
perl -MFile::Spec -e'print((File::Spec->splitdir($ENV{_pwd}))[1],"\n")'

You should always use the modules that come with Perl where possible. For a list of them, see perldoc perlmodlib.


Since \w doesen't have a forward slash in its class, why do you need [^\/] ?

/(\w+)/ will do. It captures the first occurance of this class.

edit: /.*\b(\w+)/ to capture the last occurance.


The (\w+)group matches and captures the word characters "Unix_Volume" greedily, leaving the position at the / after "Unix_Volume".

The [^\/] class forces the engine to back up (the greedy + quantifier gives up characters it's matched to satisfy atoms that follow it) to match a character that is not "/", matching the "e" at the end of "Unix_Volume". Since the matched "e" is outside the capturing group you're left with "Unix_Volum" in $1.

0

精彩评论

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

关注公众号