Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Parse string and extract word after specific pattern

by vrk (Chaplain)
on Mar 23, 2017 at 10:59 UTC ( [id://1185610]=note: print w/replies, xml ) Need Help??


in reply to Parse string and extract word after specific pattern

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.)

Replies are listed 'Best First'.
Re^2: Parse string and extract word after specific pattern
by rinkish85 (Novice) on Mar 23, 2017 at 11:02 UTC
    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!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-26 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found