zvika
zvika

Reputation: 13

Replacing all words except one - regex perl

i try to have a regular expresion which will change "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.

Upvotes: 1

Views: 478

Answers (3)

ysth
ysth

Reputation: 98378

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

Upvotes: 2

codaddict
codaddict

Reputation: 454922

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

Upvotes: 2

snoofkin
snoofkin

Reputation: 8895

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

Upvotes: 2

Related Questions