Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Parse string and extract word after specific pattern

by injunjoel (Priest)
on Mar 23, 2017 at 21:35 UTC ( [id://1185682]=note: print w/replies, xml ) Need Help??


in reply to Parse string and extract word after specific pattern

Not that fancy, but straightforward.

#!/usr/bin/perl -w use strict; my $string = "stash_ST1.2.3.4_1.ROLLBACK.check.20170228-101051.435944. +txt"; my $action = ''; my @parts = split(/\./, $string); # split on dot for (0..$#parts){ # slot after ROLLBACK $action = $parts[$_+1] if $parts[$_] eq "ROLLBACK"; } print "$action\n";

I'm sure this can be golfed down a bit.

"I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forgo their use." -Galileo

Replies are listed 'Best First'.
Re^2: Parse string and extract word after specific pattern
by RonW (Parson) on Mar 24, 2017 at 18:55 UTC

    FWIW, split(/\./, $string) can be rewritten as split('.', $string) which avoids using a regular expression.

      It also avoids the result, though.

      my $s = 'a.b.c'; say "1: $_" for split /\./, $s; say "2: $_" for split '.', $s;
      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        Apparently, the first argument to split is always treated as a regex, because split '\.', $s worked.

        The documentation implies a string and a regex are treated differently:

        (such as ' ' or "\x20" , but not e.g. / / )

        Seems that, as luck would have it, I never happened to use a regex meta character split 'x'.

        In light of the described differences in handling of ' ' vs / /, the documentation should state that the first argument is always treated as a regex even supplied as a string.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-04-19 16:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found