Yaerox Yaerox - 7 days ago 5
Perl Question

RegExp - match from root or root + 1 subdir

I'm checking an ZIP-Archive in perl if the containing folder/structure is fine or not. Till now, the root directory contained a sourcen directory

/sourcen
. RegExp
^sourcen
did this for me.

Now I have a new case. The root directory might contain one subdir before sourcen-dir so it can look like this:

/sourcen
/SUBDIR/sourcen


but it may not have more then one subdir like this:

/SUBDIR/SUBDIR/sourcen
/SUBDIR/SUBDIR/SUBDIR/sourcen
/SUBDIR/SUBDIR/SUBDIR/SUBDIR/sourcen
...


Is it possible to use RegExp to validate sourcen dir is in root or only one subdir 'lower'?

Answer

To check for /sourcen or /other/sourcen

$dir =~ m|^/ (?: [^/]+ / )? sourcen|x;

The /x allows spaces inside, for readability. The | is used instead of the customary / so that we can use / in the pattern without escaping it.

To be able to tell which is the case capture the whole thing (or its parts), by adding ().


We start with ^/ since this starts at root.

The [^/]+ checks for any character which is not /, once or more times (+). So it matches a string of characters up to the first /. The construct [^...] is called negated character class. See it in perlretut and in perlrecharclass. This is followed by literal /, needed with another directory.

All this is inside (?: ) for grouping so that it can be made optional by the following ? (zero times or once). The ?: is there so that it is not captured by (), but only grouped. This is the optional first directory.