Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Help with Multiple line RegEx

by gargle (Chaplain)
on Oct 10, 2005 at 10:57 UTC ( [id://498767]=note: print w/replies, xml ) Need Help??


in reply to Help with Multiple line RegEx

Hi,

What about the following:

#!/usr/bin/perl use strict; use warnings; while (<DATA>) { # only check between --- show switchname --- and ***** if ( /^-+ show switchname -+/ .. /^\*+/ ) { # only pick up the word between these two lines print "$1\n" if /(^\w+)/; } } __DATA__ show tech-support details -------------------- show switchname ------------------ CCC217_ANG_GREEN ****************************************************************** show tech-support details -------------------- show switchname ------------------ CCC218_ANG_GREEN ****************************************************************** show tech-support details -------------------- show switchname ------------------ CCC219_ANG_GREEN ****************************************************************** show tech-support details -------------------- show switchname ------------------ CCC220_ANG_GREEN ******************************************************************

The code indicates clearly what it's about. We use .. to only look at 'blocks' in the file and then process the block lines with a regexp looking up a single word (the first word on the line actually).

---- show ---- doesn't begin with a word, and,

************** isn't a word either.

Using .. makes updating the code later on very easy. (Imagine that there are 2 lines between ---- and ****

--
if ( 1 ) { $postman->ring() for (1..2); }

Replies are listed 'Best First'.
Re^2: Help with Multiple line RegEx
by Skeeve (Parson) on Oct 10, 2005 at 11:13 UTC
    That's it almost. The OP wanted to catch the data between those lines. So, modifying your code, something like this should work:
    if ( my $hit= /^-+ show switchname -+/ .. /^\*+/ ) { next if $hit==1 or $hit =~ /e0$/i; # When we're here, it's one of those lines in between # Now do whatever you want with the data.... } <!-- Node text goes above. Div tags should contain sig only --> <div class="pmsig"><div class="pmsig-253915"> <hr><code>$\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print
Re^2: Help with Multiple line RegEx
by blackadder (Hermit) on Oct 10, 2005 at 11:32 UTC
    Yes, This is good

    But I think it can all be done in one liner!

    Like Browser_UK suggestion.
    $Rec->{$1} = $2 if ($_=~ /-+\s+show (switchname)\s+-+\n(\w+)\n\*+/mg);
    Blackadder

      TIMTOWTDI

      It's easy to write code that a computer can read, it's not so easy to write code that a human can understand.

      It's a choice. The 'select' can easily be written as a single regexp. But think about later? What's easier to adapt? The single statement or the multiple statement?

      I, for one, like code that shows its intend, even when coding in perl ;)

      --
      if ( 1 ) { $postman->ring() for (1..2); }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-21 10:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found