matching multiple lines as one record

Discussion in 'Perl Misc' started by Stephen Moon, Feb 27, 2004.

  1. Stephen Moon

    Stephen Moon Guest

    I have a file containing the following data. How would you use
    regular expression to match the record from the header before the next
    header.

    G11: AD113167 #beginnning of header

    Freq Mag
    ----------------------------------
    0.00000 0.0002430974787725
    0.01987 0.0002434897808872
    ...
    G22: AD113168 #start of next header

    Freq Mag
    ----------------------------------
    0.09934 0.0000005524295687
    0.11921 0.0000005192898866
    0.13908 0.0000003088175192
    ...
    G33: AD113169
    ...

    $header =~ s/\A(.*?: \w+\s*)//
    $record =~ s/\s*(.*?)\s+(\S+)/$1,$2/
    I used the above for the header and the data.
    It seems like '.' doesn't match new lines and '^' and '$' only works
    for
    a string.

    Thanks in advance.

    -Steve
    Stephen Moon, Feb 27, 2004
    #1
    1. Advertising

  2. In article <>,
    Stephen Moon <> wrote:
    :I have a file containing the following data. How would you use
    :regular expression to match the record from the header before the next
    :header.

    Please see the 's' and 'm' modifiers of the 's' operator.
    --
    Feep if you love VT-52's.
    Walter Roberson, Feb 27, 2004
    #2
    1. Advertising

  3. Stephen Moon <> wrote:

    > It seems like '.' doesn't match new lines



    What's with the "seems like"?

    That is what the docs say that dot means.

    They also say how to make dot match newlines...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Feb 27, 2004
    #3
  4. Stephen Moon

    Brad Baxter Guest

    On Thu, 26 Feb 2004, Stephen Moon wrote:
    > I have a file containing the following data. How would you use
    > regular expression to match the record from the header before the next
    > header.


    Your subject says, "multiple lines as one record" but your note says, "I
    have a file". Maybe the file answer would simply things.

    while ( <DATA> ) {
    /([^:]+):\s(\w+)/ and do{ print "Header: $1,$2\n"; next };
    /(\d+\.\d+)\s+(\d+\.\d+)/ and do{ print "Record: $1,$2\n"; next };
    }

    __DATA__
    G11: AD113167 #beginnning of header

    Freq Mag
    ----------------------------------
    0.00000 0.0002430974787725
    0.01987 0.0002434897808872

    G22: AD113168 #start of next header

    Freq Mag
    ----------------------------------
    0.09934 0.0000005524295687
    0.11921 0.0000005192898866
    0.13908 0.0000003088175192

    G33: AD113169

    __END__


    Regards,

    Brad
    Brad Baxter, Mar 3, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack
    Replies:
    9
    Views:
    2,635
  2. lcs Mixmaster Remailer

    Matching multiple lines with regexp

    lcs Mixmaster Remailer, Feb 11, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    80
    Tad McClellan
    Feb 11, 2004
  3. H.S.
    Replies:
    9
    Views:
    108
  4. cyborg

    matching over multiple lines

    cyborg, Nov 21, 2006, in forum: Perl Misc
    Replies:
    4
    Views:
    164
    Mumia W. (reading news)
    Nov 21, 2006
  5. Bobby Chamness
    Replies:
    2
    Views:
    211
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page