开发者

Replacing all words except one - regex perl

i try to have a regular expresion which will ch开发者_如何学JAVAange "dir1/dir2/" to "dir1/dir3/" only if dir2 is not tmp, and if it is tmp ,I want it not to change and stay "dir1/tmp".

I think I need a lookahead regex but I cant manage.


You could use if statement before applying the s////.... check using m// if tmp is not there and then replace.


You can use negative lookahead assertion to do the substitution only if there is not tmp after /dir1:

s#(dir1/)(?!tmp/)[^/]+#\1/dir3#;

See it


s{^([^/]+)/(?!tmp/)[^/]+/}{$1/dir3/}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜