another take - using much of the same constructs as the OP, but also being aware of Tachyon's advice on .* - generally if you use a *, think twice. e.g. you originally used \d*\.\d*. - but if you tink about it, you probably done want to match '..' which \d* match (zero or more matches)
#!/usr/bin/perl -w use strict; my @buf = <DATA>; chomp @buf; # chop of all the line endings my $doc = join ' ', @buf; # one nice juicy line my $doc_id = qr(\d+\.\d+\.); # precomp regex - also self documenting while ($doc =~ /($doc_id)\s+(\w+)(.*?)Prototype:.*?(?=$doc_id)?/g) { print "$1 $2 $3\n"; } __DATA__ 5.1. GetAFunctionHere This is the description of a function Which spans a few lines Prototype: int GetAFunctionHere(int *count) Parameters: *count probably some kind of spelling object 5.2. GetMoreFunctionsHere This is the description of another function Which spans a few more lines Prototype: int GetMoreFunctionHere(float *crash) Parameters: *crash probably some kind of rowing object
the (?=...) is a fancy way of saying 'look ahead till you find another doc_id string, but also start the next match from that doc_id' - it is called a zero-width positive look ahead assertion - zero width means it does not go into the $& match string (I think)(strings are normally consumed by $& as they match), positive means find this pattern in the source string (as opposed to 'find not this pattern in the string'), look ahead means peek forward, but do not move the regex internal position counter (accessible via pos()) and assertion, well that just seems redundant - like you'd go to all that trouble then say 'but that's just a suggestion'. I put the trailing ? after the (?=...) to catch the last doc string

In reply to Re: regex help by leriksen
in thread regex help by jai

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.