Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

regex on previous lines

by MacScissor (Acolyte)
on Jan 31, 2013 at 16:01 UTC ( #1016346=perlquestion: print w/replies, xml ) Need Help??
MacScissor has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks! I have 3 lines of strings:
xxxxx yyyyy zzzzz
Now, I want to do a Regex on the last line (zzzzz). When successful, I want to do a Regex on the middle line (yyyyy). If this is successful too, I want to do a Regex on the top line (xxxxx). Can anyone tell me how to do that? Thank you in advance!

Replies are listed 'Best First'.
Re: regex on previous lines
by choroba (Bishop) on Jan 31, 2013 at 16:36 UTC
    What do you mean by "do a Regex"? Do you want to match the lines agains regular expresisons, or do you want to modify the lines via substitution?

    As usually, there are more ways how to do that. For example, if you are in a *nix environment, you can tac your file and process it from the last line. Or, you can remember the last two lines in case you want to process them:

    my @lines; while (<>) { push @lines, $_; shift @lines if @lines > 3; } if ($lines[2] =~ $regex1) { if ($lines[1] =~ $regex2) { if ($lines[0] =~ $regex3) { # Do your work here. } } }
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: regex on previous lines
by moritz (Cardinal) on Jan 31, 2013 at 17:03 UTC
Re: regex on previous lines
by si_lence (Deacon) on Jan 31, 2013 at 17:16 UTC
    another possibilty is to read the whole file in one string and match it there
    use strict; use warnings; #slurp mode, read whole file in one variable $/ = undef; my $text = <DATA>; if ($text =~ /xxxxx.*?\n.*?yyyyy.*?\n.*?zzzzz/) { print "match \n"; } else { print "no match \n"; } __DATA__ xxxxx bla yyyyy bla bla zzzzz
    if you want to match only if the three lines are excatly xxxxx, yyyyy and zzzzz respectivly, remove the .*? from the regex
Re: regex on previous lines
by LanX (Bishop) on Jan 31, 2013 at 17:28 UTC
    read lines reversed into an array

    @lines=reverse <$fh>


    @lines=reverse split /\n/, $multiline

    Now you can loop over @lines and exit with last whenever a regex fails.

    Cheers Rolf

Re: regex on previous lines
by talexb (Canon) on Jan 31, 2013 at 17:28 UTC

    I've used Tie::File successfully to tie an array to a file .. then you can just scan through the array until you find your 'zzzzz' match, then check the previous element for 'yyyyy', and if successful, the one before that for 'xxxxx'.

    As always, There's More Than One Way To Do It. :)

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: regex on previous lines
by sundialsvc4 (Abbot) on Jan 31, 2013 at 22:45 UTC

    This is, or could be, quite easy.   First, read (or otherwise get ...) all three strings.   (Die horribly if you can’t...)   Simply put them into three variables ... let’s call ’em $x, $y, $z.

    I said it would be easy....

    if ($y ~= /regex2/) { if ($y =~ /regex1/) { ... feel free ... } }

    Perhaps you would instead decide to keep the three strings in an array.   In that case, $x would be $array[0] and so-on.   If you want to maintain a three-string “window” on a file, functions such as length(), push, shift will come in handy.

    If you will please give us more details about exactly what you’re up to, The Monks will be pleased to help you more.   “Ask a more-precise question, get a more-precise answer, and, hey, both of us can call it a day and go to the pub.”

Re: regex on previous lines (or previous lines, or FIFO array)
by Anonymous Monk on Feb 01, 2013 at 10:05 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1016346]
Approved by Ratazong
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2018-04-19 21:56 GMT
Find Nodes?
    Voting Booth?