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

Hi,

Somebody have any ideas how to read this information using regular expression ?

Required Bearer Service : 255 (1213 213) Carer Service : 25 (123 13)
Thank you,
BAHARIN

update (broquaint): dropped <pre> tags and added formatting

edited by ybiC 2003-07-22: retitle from "Regular expression"

Replies are listed 'Best First'.
Re: Reqular expression
by Skeeve (Parson) on Jul 22, 2003 at 07:28 UTC
    Easy answer:
    /.*/
    ;-)

    Longer answer:

    ($name, $num1, $num2, $num3)= /^([^:]*?)\s+:\s+(\d+)\s+\((\d+)\s+(\d+) +\)/;
    Or, if you prefer to do it in 2 steps:
    ($name)= /^([^:]*?)\s+:/; @nums= /(\d+)/g

    Just some of my thoughts in the tradition of TMTOWTDI ;-)

      Thanks bro,
      
      But, some of them is null, how can I get it ?, example: 
      Required Bearer Capability  : ><
      
      
Re: Reqular expression
by pzbagel (Chaplain) on Jul 22, 2003 at 07:01 UTC

    Depends on what you want exactly, this extracts the names and number into seperate variables:

    @data=/^([\w ]+?)\s+:\s+(\d+)\s+\((\d+)\s+(\d+)\)/;

    If you can guarantee that some of those spaces will always be single spaces, you can change many of the \s+ into plain spaces " "

    HTH

Re: Reqular expression
by bart (Canon) on Jul 22, 2003 at 11:26 UTC
    This is a continuing story, isn't it? (Note to other users: bh_perl has written a few earlier threads on processing this or very similar data.)

    Anyway... must this be a regular expression? Id' rather use split.

    s/\s+$//; #remove trailing whitespace -- and newlines! my($name, $value) = split /\s*:\s*/, $_, 2;
    The 2 as a third argument to split prevents the value from getting broken up in case it contains a colon, as split now always will split into two parts: the name, and the value.
Re: Parse colon-delimited data
by beernuts (Pilgrim) on Jul 23, 2003 at 21:24 UTC

    Is there any reason it has to be a regexp? Not that it isn't an interesting regexp problem, but Text::CSV_XS with 'sep_char' set to ':' should solve your problem - as well as several of your other delimited-text processing problems. Something like this (from `perldoc Text::CSV_XS`):

    $csv = Text::CSV_XS->new({
       'quote_char' => '"',
       'escape_char' => '"',
       'sep_char' => ':',
       'binary' => 0
    });

    It's quite flexible...

    -beernuts