Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

regex needed with match operator

by Anonymous Monk
on Apr 28, 2003 at 13:28 UTC ( #253678=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks,
I'm trying to extract the text between the slashes just before the end of the text foo.
C:/WINDOWS/TEST/Vendor/Bomb_USR/foo C:/WINDOWS/TEST/Vendor/Bomb_MOT/foo C:/WINDOWS/TEST/Vendor/Bay_Accelar/foo C:/WINDOWS/TEST/Vendor/BayStackHub/foo C:/WINDOWS/TEST/Vendor/Ascend_4000/foo C:/WINDOWS/TEST/Vendor/Aironet/foo C:/WINDOWS/TEST/Vendor/ADC_Cuda/foo C:/WINDOWS/TEST/Vendor/3ComTokenRing/foo
So I'm using this code to try and extract the following list:
Bomb_USR Bomb_MOT Bay_Accelar BayStackHub Ascend_4000 Aironet ADC_Cuda 3ComTokenRing foreach (@FILES) { if m[\/\w+foo\$] { $_ =~ s/foo//; push @array, $_; } } print "$_\n" for @array;
What am I doing wrong?

Replies are listed 'Best First'.
Re: regex needed with match operator
by broquaint (Abbot) on Apr 28, 2003 at 13:37 UTC
    Here's a fix for your regex
    if(m[/\w+/foo]) { s[.* /(\w+) /foo \z]($1)x; ... }
    Here's a simpler solution which grabs what is matched (as opposed to modifying the contents @FILES as your code does)
    print m{ /(\w+) /foo\z }x, "\n" for @FILES;
    See. perlre for more info on the regex used.
    HTH

    _________
    broquaint

      And once you try to store the retrieved bits, you arrive at something like
      my @name; push @name, m{ /(\w+) /foo\z }x for @FILES;
      which, of course, is a map in disguise:
      my @name = map m{ /(\w+) /foo\z }x, @FILES;

      Makeshifts last the longest.

      Thanks broquaint,
      Would you mind explaining exactly what the \z and x are doing in the regex?

      Thanks again

        Would you mind explaining exactly what the \z and x are doing in the regex?
        Sure, to quote the docs
        \z Match only at end of string
        This is very similar to $ except $ matches the end of a line or before a newline.
        x Extend your pattern's legibility by permitting whitespace and comments.
        As it says, it allows for more readable regex, which is generally desirable considering the acute succinctness of regular expressions.
        HTH

        _________
        broquaint

Re: regex needed with match operator
by rob_au (Abbot) on Apr 28, 2003 at 13:41 UTC
    The most portable manner for dealing with file names and directory paths is through the use of File::Spec module - For example:

    use File::Spec; my @paths = ( 'C:/WINDOWS/TEST/Vendor/Bomb_USR/foo', 'C:/WINDOWS/TEST/Vendor/Bomb_MOT/foo', 'C:/WINDOWS/TEST/Vendor/Bay_Accelar/foo', 'C:/WINDOWS/TEST/Vendor/BayStackHub/foo', 'C:/WINDOWS/TEST/Vendor/Ascend_4000/foo', 'C:/WINDOWS/TEST/Vendor/Aironet/foo', 'C:/WINDOWS/TEST/Vendor/ADC_Cuda/foo', 'C:/WINDOWS/TEST/Vendor/3ComTokenRing/foo', ); foreach my $path ( @paths ) { my $value = ( File::Spec->splitdir( $path ) )[-2]; print $value, "\n"; }

     

    perl -le 'print+unpack("N",pack("B32","00000000000000000000001001010011"))'

Re: regex needed with match operator
by LordWeber (Monk) on Apr 28, 2003 at 13:36 UTC
        /\/(\w+)\/foo$/

        Perhaps better as:

        m!/([^/]+)/foo$!

        Advantages:

        • Uses "choose your own quotes" to avoid falling-toothpick syndrome.
        • Some systems allow some funky characters that \w won't match. However, '/' certainly won't be in a filename (unless something really, really weird happens).

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        Note: All code is untested, unless otherwise stated

          In fact, the null byte is the only character you can expect not to show up in file names; on Unixoid and DOSish systems, the slash is also guaranteed not to appear, unless maybe you're working with a shot filesystem (I don't think we need to discuss that case). DOSish systems impose a whole lot of additional restrictions of course; none of those exist on Unixoid systems. On other systems than those, all bets are off; I wouldn't be surprised if the slash was a valid part of filenames on something "weird" like VMS.

          Makeshifts last the longest.

    Re: regex needed with match operator
    by LordWeber (Monk) on Apr 28, 2003 at 16:06 UTC
          Read Aristotle's sig. Every application I've ever worked on started out as a "quick and dirty" script. It's always best, unless you are deleting the script within 12 hours, to use the modules.
          1. You know it will catch the boundary cases you didn't know even existed
          2. It's quicker
          3. The more of your <whatever> is stock code, the less you have to debug.

          ------
          We are the carpenters and bricklayers of the Information Age.

          Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

          Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Log In?
      Username:
      Password:

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

      How do I use this? | Other CB clients
      Other Users?
      Others wandering the Monastery: (7)
      As of 2021-04-21 12:03 GMT
      Sections?
      Information?
      Find Nodes?
      Leftovers?
        Voting Booth?

        No recent polls found

        Notices?