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

Re: Regex returns match the first time. It returns everything thereafter

by kcott (Abbot)
on Nov 15, 2013 at 18:47 UTC ( #1062773=note: print w/ replies, xml ) Need Help??


in reply to Regex returns match the first time. It returns everything thereafter

G'day guitarplayer68,

The base problem is that when /line/ matches, the range operator becomes TRUE; thereafter, it never becomes FALSE. See perlop: Range Operators for a full description of the flip-flop boolean states of this operator.

The more specific problem (i.e. why the operator doesn't become FALSE), would be that /\\Z/ never matches. What that matches is a literal backslash (\\) followed by a capital zed (Z). I suspect that's not what you meant. I further suspect that you're perhaps confused with \Z which matches the end of a string (possibly before a newline if one exists). See Assertions under perlre: Regular Expressions. Unfortunately, \Z is not what you want either.

From the data you've shown, there doesn't appear to be anything you can use to match the last line in each file. Given this, you might be better off changing your code to something with logic along these lines:

#!/usr/bin/env perl -l use strict; use warnings; my @test_data = ( [qw{discardA1 discardA2 lineA wantedA1 wantedA2}], [qw{discardB1 discardB2 lineB wantedB1 wantedB2}], [qw{discardC1 discardC2 lineC wantedC1 wantedC2}], ); for (@test_data) { my @ssh = @$_; my $wanted_found = 0; for my $line (@ssh) { if (! $wanted_found) { $wanted_found = $line =~ /line/; } else { print $line; } } }

Output:

wantedA1 wantedA2 wantedB1 wantedB2 wantedC1 wantedC2

If you also wanted the line that matched /line/, you can change that logic to this:

#!/usr/bin/env perl -l use strict; use warnings; my @test_data = ( [qw{discardA1 discardA2 lineA wantedA1 wantedA2}], [qw{discardB1 discardB2 lineB wantedB1 wantedB2}], [qw{discardC1 discardC2 lineC wantedC1 wantedC2}], ); for (@test_data) { my @ssh = @$_; my $wanted_found = 0; for my $line (@ssh) { if (! $wanted_found) { next unless $wanted_found = $line =~ /line/; } print $line; } }

Output:

lineA wantedA1 wantedA2 lineB wantedB1 wantedB2 lineC wantedC1 wantedC2

-- Ken


Comment on Re: Regex returns match the first time. It returns everything thereafter
Select or Download Code
Re^2: Regex returns match the first time. It returns everything thereafter
by Laurent_R (Parson) on Nov 15, 2013 at 19:46 UTC
    Just one word to say that I tried earlier to post a message saying that your second term of the flip flop operator is probably never matched, so that the operator remains always true, but it was impossible to post a message at the time. So, basically, I fully agree with previous comments.
Re^2: Regex returns match the first time. It returns everything thereafter
by guitarplayer68 (Novice) on Nov 15, 2013 at 20:53 UTC

    Ken
    I didn't notice the extra \ when I was typing this up. Sorry for the confusion.
    It is not that I am trying to match the last line. I am trying to get everything after the line "customizations must be after this line." and ignore the rest.
    Thank you for your reply

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-09-30 22:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (385 votes), past polls