开发者

Replacing all words except one - regex perl

开发者 https://www.devze.com 2023-02-06 11:25 出处:网络
i try to have a regular expresion which will ch开发者_如何学JAVAange \"dir1/dir2/\" to \"dir1/dir3/\"

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

精彩评论

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

关注公众号