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/}
精彩评论