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
To check for
$dir =~ m|^/ (?: [^/]+ / )? sourcen|x;
/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.
[^/]+ 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.