in reply to Probably silly regex question / not so silly regex question

Well, not all file names have extensions, so you might want one of these:

( $base, $ext )= $file =~ /^(.*?)([.][^.]*)?$/s; $ext= ( $base= $file ) =~ s/([.][^.]*)$// ? $1 : ""; $base= substr( $ext= $file, 0, 0xffff & rindex($file,"."), "" );

/.*?c/s and /[^c]*c/ will always (I'm pretty sure) match the same thing (note the addition of "s" to the first one) as they stand. But you were using these as part of a larger regex, and there they don't always match the same thing. For example, /.*?cd/s and /[^c]*cd/ (adding a "d" to each one) don't always match the same things. On the string "abcabcd", the first will match "abcabcd" while the second will only match "abcd".

Replies are listed 'Best First'.
Re: SOLUTION : Not so silly regex question
by Anonymous Monk on Aug 02, 2000 at 01:29 UTC

    Ah, thank you (for your second point); that finaly clears it up. (All the other answers didn't parse the question as I meant it.)

    As for the second part, since all these files have a name of the form a-z+-0-9+a-z0-9-*.jpg, so I just used a simple split /\./. (They're all jpegs now, but that might change; I wanted it to be at least a little more generic.) Your point is, of course, well taken, though there's probably a large problem space where you do know that there is an extention.

    Thanks also to all the other people that had solutions to the $id.$ext problem as well; your answers were almost all enlightening.

    I still wonder which is more efficent. I'll probably do some benchmarking at some later point.

    Thanks,
    James Mastros,
    Just Another Perlmonks Neonate