FeistyLemur has asked for the wisdom of the Perl Monks concerning the following question:
This is my first post here, I have a question about something I can't explain.
I'm pretty sure I know what I did wrong, I'm just not 100% sure why it was wrong and wanted to clarify to better my understanding of regex in perl, as I'm fairly inexperienced and looking to improve.
I was searching for vlan ids for removal in the output of "ip addr" in the following way.
my $ethernet=`ip addr`; if ($ethernet!~/eth1\.\d{4}\@/gm){ print "No vlans exist on device.\n"; exit; } while ($ethernet=~/(eth1\.\d{4})\@/gm) { print `ip link del $1\n"; print "Removed $1\n"; }
Doing this as above mostly worked, but it would always miss the first entry for no reason I can explain. So if there were 25 vlan entries in the string from 1001 to 1025 it would match and delete 1002-1025 without fail, and miss 1001 every time.
Changing line 2 to:
if ($ethernet!~/eth1\.\d{4}\@/){Does what I intended to do.
I had the same problem with a similar line I was using to scrub IP addresses off the vlan devices again missing the first match, I just don't understand why the while loop breaks in the way it does because of the /gm flag on the preceeding if statements match, and was hoping someone could explain. Thanks in advance.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: A regex question.
by AnomalousMonk (Archbishop) on Jul 31, 2015 at 22:00 UTC | |
by Anonymous Monk on Jul 31, 2015 at 23:57 UTC | |
by Athanasius (Archbishop) on Aug 01, 2015 at 03:23 UTC | |
by FeistyLemur (Acolyte) on Aug 02, 2015 at 05:15 UTC | |
by AnomalousMonk (Archbishop) on Aug 01, 2015 at 08:51 UTC | |
|
Re: A regex question.
by Aldebaran (Curate) on Aug 01, 2015 at 08:52 UTC | |
by 1nickt (Canon) on Aug 01, 2015 at 14:42 UTC |