Replace everything except the last using perl

I have a say string "a/b/c/d/e"and I want to replace everything except the last /with. ie my conclusion should be a.b.c.d/e.

How do I change my lookup command s/\//./gto do the same? Also note that the hierarchy does not always have 4 " /". It can be any number.

+4
source share
1 answer

Replace all /for which there are /lower in the chain; so it excludes the last/

perl -wE'$_ = q(a/b/c/d/e); s{/(?=.*?/)}{.}g; say'

It uses positive "outlook" , (?=...)to assert the presence of /further (after .*?), without consuming anything.


- ( ) / "lookahead"

s{/([^/]*)(?=/)}{.$1}g;

, , / "" , . ([^/]*) ( ), (.*? ).

: 126% v5.10.1 v5.27.2 ( ) 110% v5.16.1 ( ). 4-10 .

ikegami , , .

+8

Source: https://habr.com/ru/post/1687687/


All Articles