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

Re^2: A regex question.

by Anonymous Monk
on Jul 31, 2015 at 23:57 UTC ( [id://1137088]=note: print w/replies, xml ) Need Help??


in reply to Re: A regex question.
in thread A regex question.

Hm, well I really didn't understand that about how the match operator works so I guess that would explain it. I had assumed /g was pointless on the if after thinking about it, but the part I really couldn't figure out was why the negative match would affect the subsequent one in any way.

Replies are listed 'Best First'.
Re^3: A regex question.
by Athanasius (Archbishop) on Aug 01, 2015 at 03:23 UTC

    Hello FeistyLemur, and welcome to the Monastery!

    See the section “Global matching” in perlretut#Using-regular-expressions-in-Perl, and in particular note the following (emphasis added.):

    In scalar context, successive invocations against a string will have //g jump from match to match, keeping track of position in the string as it goes along....
    A failed match or changing the target string resets the position.... The current position in the string is associated with the string, not the regexp.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Yes, I understand now why I was seeing the behavior in question. Things are working as I intended in my script. I just wanted to understand what I was missing about the match operator. I didn't realize the position in the string was associated with the string and not the regexp.

      I got the answer I was looking for and understand the match operator better now, thanks.

Re^3: A regex question.
by AnomalousMonk (Archbishop) on Aug 01, 2015 at 08:51 UTC
    ... the negative match ...

    It's important to understand that the negation is made on the result of the  /eth1\.\d{4}\@/gm match against the  $ethernet string. The match itself is actually successful! It's as if the
        if ($ethernet !~ /eth1\.\d{4}\@/gm) { ... }
    statement had been written as
        if ( ! ($ethernet =~ /eth1\.\d{4}\@/gm)) { ... }
    (note =~ vice !~). In fact, that's pretty much the way Perl sees the code:

    c:\@Work\Perl\monks>perl -wMstrict -MO=Deparse,-p -le "my $ethernet=`ip addr`; if ($ethernet!~/eth1\.\d{4}\@/gm){ print qq{No vlans exist on device.\n}; exit; } " BEGIN { $^W = 1; } BEGIN { $/ = "\n"; $\ = "\n"; } use strict 'refs'; (my $ethernet = `ip addr`); unless (($ethernet =~ /eth1\.\d{4}\@/gm)) { print("No vlans exist on device.\n"); exit; } -e syntax OK

    I had assumed /g was pointless on the if ...

    Given what you were trying to do, it was pointless, but it was not without effect!


    Give a man a fish:  <%-(-(-(-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-03-19 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found