Lloyd T Brown wrote:

Here's an example of the format I decided to use.

"filename: somefilename.txt;mime-type: text/plain; data: VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw=="

So, I think the regexp that would allow me to extract the filename, mime-type, and the data itself, is this:

/^filename:\s+([-_.a-zA-Z0-9]+);mime-type:\s+([-_/a-zA-Z0-9]+);data:\s(.*)$/

I tend to prefer to err on the lax side when it comes to regexes and such, and in a case like this I would replace the strict (as you said yourself, possibly incorrect) character classes with looser negative classes. Also, take advantage of what you know -- the whitespace that comes after 'filename:', 'mime-type:' and 'data:' is put there by you, so you know it will be just one ' ' character -- say so in your regex. Finally, rather than make the regex match all the (.*) on the end (not a heavy penalty, but still...) just remove everything else and keep that as the data. So my version of the regex would be:


s/^filename: ([^;]+);mime-type: ([^;]+);data: //;

Then catch the filename and mime-type from $1 and $2 (or their eq. in PHP or whatever you're using). So if I were doing it in perl:

sub parse_record
{
   my $record = shift;
   $record =~ s/^filename: ([^;]+);mime-type: ([^;]+);data: //
      or warn "Record doesn't match expected format";
   return ($1, $2, $record);
}

# usage
my ($filename, $mimetype, $data) = &parse_record($record);

Jacob Fugal


____________________
BYU Unix Users Group http://uug.byu.edu/ ___________________________________________________________________
List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list

Reply via email to