Re: dir match, dir may or may not appear
by Corion (Patriarch) on Jan 12, 2016 at 08:58 UTC
|
What syntax error and where? Perl is usually quite informative with its error messages.
Most likely, it's a syntax error in your regular expression. See perlre, especially about quantifiers.
Personally, I would reformulate the problem as "any directory that starts with /usr/ and ends with /bin, and do the checking in two regular expressions instead.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
Nested quantifiers in regex marked by <-- HERE in m/^/usr/(/[^/]+{ <-
+- HERE 0,1})/bin//
Two expressions is fine,is there a one liner, as this could increase in future...? | [reply] [Watch: Dir/Any] [d/l] |
|
Yes, see perlre. If one expression is too difficult, why not use two?
If you really need to write it in one expression, use a group and quantify that:
m!^/usr(?:/[^/]+)?/bin!;
| [reply] [Watch: Dir/Any] [d/l] |
|
a quantifier is +
a quantifier is {0,1}
You can't have {0,1}{0,1}{0,1}{0,1}
You can't have +{0,1}+{0,1}
If you want a pattern ([^/]+) to repeat, you have to group it
perlre, perlretut, perlrequick describe these words I've used, go fish
| [reply] [Watch: Dir/Any] [d/l] |
Re: dir match, dir may or may not appear
by GrandFather (Saint) on Jan 12, 2016 at 10:04 UTC
|
/usr/local/bin is a special case of /usr/*anydir*/bin so the various answers you were given in the CB would do the job. Here's one:
$path =~ m~^/usr (?: (?:/[^/\0]+|\\/)+ )? /bin~x;
that uses elements of my and tye's CB answers.
Premature optimization is the root of all job security
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: dir match, dir may or may not appear
by QuillMeantTen (Friar) on Jan 12, 2016 at 09:17 UTC
|
In your regex [^/]+{0,1} does not make sense if I understand it correctly, basically you say
anything but a slash, at least one time, then you say at least 0 time at most 1... you gotta choose which you want, either it is
[^/]+ or
[^/]? (equivalent to {0,1})
For what you want to do I'd say:
m#\A/usr/([^/]+/)?bin#
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] [d/l] |
Re: dir match, dir may or may not appear
by hellosarathy (Novice) on Jan 12, 2016 at 10:26 UTC
|
Thanks... what you gave worked:
$path =~ m|^usr/(?:/[^/]+)?/bin/|
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] [d/l] |