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

Hi, I need to parse the below string

stash_ST1.2.3.4_1.ROLLBACK.check.20170228-101051.435944.txt

I am intrested only in part ROLLBACK.check

Basically I am looking at what appears after "ROLLBACK" in above string.

for e.g in this case its "check" .... that could be any word string like check2, check3, migration1, migration2.

Thank you. Please suggest.
  • Comment on Parse string and extract word after specific pattern

Replies are listed 'Best First'.
Re: Parse string and extract word after specific pattern
by hippo (Archbishop) on Mar 23, 2017 at 10:53 UTC
Re: Parse string and extract word after specific pattern
by scorpio17 (Canon) on Mar 23, 2017 at 13:29 UTC
    my $string = "stash_ST1.2.3.4_1.ROLLBACK.check.20170228-101051.435944. +txt"; # find one or more not "." between pair of "."s following word ROLLBAC +K my ($word) = $string =~ m/ROLLBACK\.([^\.]+)\./; print "$word\n"; # prints "check"
Re: Parse string and extract word after specific pattern
by BillKSmith (Monsignor) on Mar 23, 2017 at 13:39 UTC
    The best answer depends on your definition of 'word'. You probably mean a string of characters that you consider as 'word characters'. Under this definition, you probably want a regular expression. Perl's character class \w is almost certainly what you need to match those characters. Although it is not necessary, I also recommend that you verify that your regex match succeeds before using its result.

    UPDATE: Note that the code that this would require is the same as scorpio17's except for the regex. He implements a different definition of 'word'.

    Bill
Re: Parse string and extract word after specific pattern
by vrk (Chaplain) on Mar 23, 2017 at 10:59 UTC

    Does the start of the string before ROLLBACK vary? If so, what other kinds of prefixes are there? Or is "ROLLBACK" always in the string before the part you're interested in? (If it is, you don't even need a regex; a simple index call followed by substr will work.)

      ROLLBACK is always a part of string and I need only the word which follows ROLLBACK.

        If that's all it is, you can try this instead of a regular expression:

        #!/usr/bin/perl use v5.10; my $string = "stash_ST1.2.3.4_1.ROLLBACK.check.20170228-101051.435944. +txt"; # Find "ROLLBACK" from the end of string, in case there's more than on +e of # them. my $i = rindex($string, 'ROLLBACK.'); if ($i >= 0) { $i += length('ROLLBACK.'); # Now $i points to the character after the full stop. # Find the position of the next full stop after it. my $j = index($string, '.', $i); if ($j >= 0) { my $word = substr($string, $i, $j - $i); say $word; } else { # No full stop after ROLLBACK -- signal an error? } } else { # ROLLBACK not found! Signal an error? }

        However, scorpio17's regular expression is better, because you can see the input string's expected structure more clearly and the regex solution is also shorter!

Re: Parse string and extract word after specific pattern
by injunjoel (Priest) on Mar 23, 2017 at 21:35 UTC

    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

      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,