I have file path that has one or more directories and like this "/abc/def/ghi". I need a regex that can break it into an array as follows
( /abc/def/ghi, /abc/def, /abc )
alternatively a reversed array will do as well
( /abc, /abc/def, /abc/def/ghi )
The following regex breaks path into up to 3 segments
my $path = "/abc/def/ghi";
my @segments = ($path =~ m'^(/[^/]+ (/[^/]+ (/[^/]+)? )? )'x);
This regex is rigid. I can make it long enough to be able to break path into N segments but if the actual path has N+1 segments the regex won't work.
I want a regex that can break path into variable number of segments and not hardcode maximum path length into it.