Replying to myself,
mwah and
graff
I'm handling POSTed data, actually I'm
reverse engineering .Mac services. This is why I'm not afraid the 'length' information can't be trusted; programmers from Cupertino wouldn't fool themselves _that_ much.
Also, as far as I've seen, there's never been more than 1 <file></file> pair. It would just have been the cherry on the cake to take that chance out.
As
if ( $indata =~ s{<file fiop="([^"]+)" length="([^"]+)"/>(.*?)</file>}
+{}s ) {
( $fiop, $fileLength, $fileData ) = ( $1, $2, $3 );
works (for now), but I definitely want to take out the chance that the binary data contains </file>, I just'd like to optimize the regex.
I _do_ know it can be done with substr, but (knowing -but not completeley understanding- the power of regex) I just wondered if a back-referencing to length within the regex would/could be possible.
Is something like:
if ( $indata =~ s{<file fiop="([^"]+)" length="([^"]+)"/>(.{$2})</file
+>}{}s ) {
( $fiop, $fileLength, $fileData ) = ( $1, $2, $3 );
possible ?
update:
As regex is limited in matching to a given (64k - or so) length; we decided to assume there's only 1 occurence of a <file/> node; we can match greedy:
if ( $indata =~ s{<file fiop="([^"]+)" length="([^"]+)"/>(.*)</file>}{
+}s ) {
( $fiop, $fileLength, $fileData ) = ( $1, $2, $3 );
(matching the final occurence of </file>)