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#!/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
In reply to Re: regex help
by leriksen
in thread regex help
by jai
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |