Forum Moderators: coopster

Message Too Old, No Replies

preg match() [function.preg-match]: Unknown modifier '/'

regex perl

         

flapane

4:27 pm on Mar 25, 2009 (gmt 0)

10+ Year Member



Hi,
I was trying to modify a script by adding a regex not only for the extension, but for the filename too so that it can parses file such as amsnfoo.deb amsnbar.deb and so on.

<?
$dir = "/foo/bar";
$pattern = '^(amsn[^/.]+\.deb)$';

$newstamp = 0;
$newname = "";
$dc = opendir($dir);
while ($fn = readdir($dc)) {
//echo $fn;
# Eliminate current directory, parent directory
if (ereg('^\.{1,2}$',$fn)) continue;
# Eliminate other pages not in pattern
if (! preg_match($pattern,$fn)) continue;
$timedat = filemtime("$dir/$fn");
if ($timedat > $newstamp) {
$newstamp = $timedat;
$newname = $fn;
}
}
# $timedat is the time for the latest file
# $newname is the name of the latest file
echo $timedat;
echo "<br />";
echo $newname;
?>

Unfortunately I get that unknown modifier error, please do you have any hints?

[edited by: phranque at 11:14 pm (utc) on Mar. 25, 2009]
[edit reason] No urls, please. See TOS [webmasterworld.com] [/edit]

phranque

9:56 pm on Mar 26, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



[^/.]

what character class are you trying to match here?
"not a literal dot" or "not dot and not slash"?

i'm guessing you should try [^.] or [^\/.] here

Special Characters Inside a Bracketed Character Class [search.cpan.org]:

Characters that may carry a special meaning inside a character class are: \, ^, -, [ and ], and are discussed below. They can be escaped with a backslash, although this is sometimes not needed, in which case the backslash may be omitted.

flapane

10:12 pm on Mar 26, 2009 (gmt 0)

10+ Year Member



Hi,
yes I have just solved in this way
$pattern = '/^(amsn_0.9[^\/.]+\.deb)$/';
furthermore there was another problem at:
if (ereg('^\.{1,2}$',$fn)) continue;

changing it with:
if ( preg_match( '/^\.\.?$/', $fn ) ) continue;

solves finally everything and the script does its job.
It seems like some / were missing; regex are relatively new for me, I hope I can improve my skill on them in the future.
Thanks

phranque

10:00 am on Mar 27, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



perlretut - (perl regular expression tutorial):
[perldoc.perl.org...]

flapane

10:14 am on Mar 27, 2009 (gmt 0)

10+ Year Member



That's great that's what I was searching for, I'll add into my favs thanks

flapane

4:18 pm on May 2, 2009 (gmt 0)

10+ Year Member



Hi
it seems that there is a little problem:

<?php
$dir = "/foo/bar";
$pattern = '/^(amsn_0.9[^\/]+.deb)$/';

$newstamp = 0;
$timedat = 0;
$newname = "";
$dc = opendir($dir);
while ($fn = readdir($dc)) {
# Eliminate current directory, parent directory
if ( preg_match( '/^..?$/', $fn ) ) continue;
# Eliminate other pages not in pattern
if (! preg_match($pattern,$fn)) continue;
$timedat = filemtime("$dir/$fn");
if ($timedat > $newstamp) {
$newstamp = $timedat;
$newname = $fn;
}
}

# $timedat is the time for the latest file
# $newname is the name of the latest file

$scrivi_data = date('j M y', $timedat);

echo "Versione pił recente compilata il: <strong> $scrivi_data </strong>";
echo "Nome: ";
echo "<strong> $newname </strong>";

?>

#newname works good and outputs the most recent filename, but #timedat doesn't show the most recent date
If I have a folder with two files:
amsn1.deb 3 apr
amsn2.deb 6 apr, the script will show:

The most recent file is amsn2.deb
Compiled on $amsn1.deb_date

While it should show the date in which amsn2.deb has been compiled.

Any hints?
thanks