开发者

unexpected results for perl regular expression

开发者 https://www.devze.com 2023-03-20 18:39 出处:网络
What is wrong开发者_开发百科 with this one? echo commons-logging.jar | perl -pe \'s/(.*?)([^s]+\\.jar)/$1 $2/\'

What is wrong开发者_开发百科 with this one?

echo commons-logging.jar | perl -pe 's/(.*?)([^s]+\.jar)/$1 $2/' 
commons -logging.jar 

I would expect $2 to capture the whole commons-logging.jar? Especially as

echo commons-logging.jar | perl -pe 's/([^s]+\.jar)/$1/' 
commons-logging.jar 


The first one says "capture everything non-greedily up until the first character thats not an "s" and put it into $1 (hence $1 = common) and than put everything after that match that ends in ".jar" into $2 (hence $2 = "-logging.jar").

The second form says "capture everything staring with at least one or more characters that are not an "s" (there4fore the capture begins at "c") followed by the something that ends in ".jar" (hence you get the whole "commons-logging.jar" in $1)


The [^s]+ matches all characters that aren't a literal 's', so the best you will get is -logging.jar


You have one regular expression. "Any number of any character (non greedy) followed by at least one character that is not s followed by .jar".

The "followed by" is important, the matches can't overlap.

try:

echo commons-logging.jar | perl -pe 's/(.*?)([^s]+\.jar)/$1 $1$2/' 
0

精彩评论

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

关注公众号