Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

dir match, dir may or may not appear

by hellosarathy (Novice)
on Jan 12, 2016 at 08:56 UTC ( #1152537=perlquestion: print w/replies, xml ) Need Help??

hellosarathy has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

How do I match:

/usr/bin /usr/local/bin /usr/*anydir*/bin *anydir* may or may not appear? my regexp: $path =~ m|^/usr/(/[^/]+{0,1})/bin/| reports syntax error.

Replies are listed 'Best First'.
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.

      the regexp has errors:
      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...?

        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!;

        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

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
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#

      Thanks,

      I meant "(slash followed by anything except slash) to occur 0 or 1 time"

      Basically 0 or 1 dir can appear inbetween /usr and bin/

        The "?" quantifier means to match 0 or 1 times as specified in the "Quantifiers" section of perlre. Therefore your pattern to match this could be simply (/[^/]+)? which is similar to QuillMeantTen's approach above.

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/|

      As a general thing don't use | as a regex delimiter because it is the alternation (or) character. It allows you to match cat or dog m~cat|dog~ for example.

      Premature optimization is the root of all job security

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1152537]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2022-07-07 00:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?