Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Command output from modem is: TxNum Band: 3 [10-30] [50-80] [100-200] RxNum Band: 3 {100-120} [120-300] [400-600] [800-1000] Others: 2000
Please help me in writing regular expression so that i can extract the + TxBands and RxBands in the following format Txbands: [10-30] [50-80] [100-200] Rxbands:[120-300] [400-600] [800-1000]

Replies are listed 'Best First'.
Re: Regular expression help required
by ww (Archbishop) on Oct 22, 2009 at 11:14 UTC
Re: Regular expression help required
by grizzley (Chaplain) on Oct 22, 2009 at 11:49 UTC

    Do you have your output in a string already? Then do following:

    s/:.*/:/g; s/\n\[/ [/g
Re: Regular expression help required
by Anonymous Monk on Oct 22, 2009 at 11:10 UTC
    FYI, regular expressions do not extract into formats
      Question is how to write a regular expression which can do backreferen +cing so that so that 3 lines can backreerenced at once.. For example: [0-100] to match this one could write (\[[0-9]+\-[0-9]+\]) and once the string is matched it can be extracte +d in using $var=$1 which should get [0-100] Now how could one write Regular expression to extract 3 lines as given + in problem description..
Re: Regular expression help required
by bichonfrise74 (Vicar) on Oct 22, 2009 at 20:34 UTC
    Try this.
    #!/usr/bin/perl use strict; my (%record, $key); while (<DATA>) { chomp; if ( /^[A-Z]/ ... /]\n[A-Z]/ ) { ($key) = /(\S+)/ if ( /^[A-Z]/ ); push( @{ $record{$key} }, $_ ) if ( !/^[A-Z]/ ); } } print "$_: @{ $record{$_} }\n" for ( keys %record ); __DATA__ TxNum Band: 3 [10-30] [50-80] [100-200] RxNum Band: 3 {100-120} [120-300] [400-600] [800-1000] Others: 2000