Connecting Tech Pros Worldwide Forums | Help | Site Map

Regular Expressions

samug
Guest
 
Posts: n/a
#1: Jul 17 '05
I have some files whose file names are like this:
asdf1.jpg
qer10.jpg
rwytew45.jpg

So, I want to rename all the files that has only one digit to
a two digit number to make them alphabetically correct in
the listing like this: adsf01.jpg

I tried to do it like this:
for($i=0; $i<count($files); $i++) {
print preg_replace("|(\w+)(\d)(\.jpg)|", "\${1}0\$2\$3", $files[$i]);
}


but it's not working. It converts all the files like this:
asdf1.jpg => asdf01.jpg
qer10.jpg => qer100.jpg
rwytew45.jpg => rwytew405.jpg

It doesn't seem to care about me wanting only one digit (\d) int the replace.
Any idea how this could be done ?

Shawn Wilson
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Regular Expressions


samug wrote:[color=blue]
>
> I have some files whose file names are like this:
> asdf1.jpg
> qer10.jpg
> rwytew45.jpg
>
> So, I want to rename all the files that has only one digit to
> a two digit number to make them alphabetically correct in
> the listing like this: adsf01.jpg
>
> I tried to do it like this:
> for($i=0; $i<count($files); $i++) {
> print preg_replace("|(\w+)(\d)(\.jpg)|", "\${1}0\$2\$3", $files[$i]);
> }
>
> but it's not working. It converts all the files like this:
> asdf1.jpg => asdf01.jpg
> qer10.jpg => qer100.jpg
> rwytew45.jpg => rwytew405.jpg
>
> It doesn't seem to care about me wanting only one digit (\d) int the replace.
> Any idea how this could be done ?[/color]

This might work:
print preg_replace("|^(\w*[^\d])(\d)(\.jpg)$|", "\${1}0\$2\$3", $files[$i]);

Regards,
Shawn
--
Shawn Wilson
shawn@glassgiant.com
http://www.glassgiant.com
Shmuel
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Regular Expressions


It didn't work until I made it like this:
print preg_replace("|^([^\d]+)(\d)(\.jpg)$|", "\${1}0\$2\$3", $str) .
"<br />";



Shawn Wilson wrote:[color=blue]
> samug wrote:
>[color=green]
>>I have some files whose file names are like this:
>>asdf1.jpg
>>qer10.jpg
>>rwytew45.jpg
>>
>>So, I want to rename all the files that has only one digit to
>>a two digit number to make them alphabetically correct in
>>the listing like this: adsf01.jpg
>>
>>I tried to do it like this:
>>for($i=0; $i<count($files); $i++) {
>> print preg_replace("|(\w+)(\d)(\.jpg)|", "\${1}0\$2\$3", $files[$i]);
>>}
>>
>>but it's not working. It converts all the files like this:
>>asdf1.jpg => asdf01.jpg
>>qer10.jpg => qer100.jpg
>>rwytew45.jpg => rwytew405.jpg
>>
>>It doesn't seem to care about me wanting only one digit (\d) int the replace.
>>Any idea how this could be done ?[/color]
>
>
> This might work:
> print preg_replace("|^(\w*[^\d])(\d)(\.jpg)$|", "\${1}0\$2\$3", $files[$i]);
>
> Regards,
> Shawn[/color]

Pedro Graca
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Regular Expressions


samug wrote:[color=blue]
> So, I want to rename all the files that has only one digit to
> a two digit number to make them alphabetically correct in
> the listing like this: adsf01.jpg
>
> I tried to do it like this:
> for($i=0; $i<count($files); $i++) {
> print preg_replace("|(\w+)(\d)(\.jpg)|", "\${1}0\$2\$3", $files[$i]);
> }
>
>
> but it's not working.[/color]

Your \w+ matches digits too!

<?php
$files = array('asdf1.jpg', 'qer10.jpg', 'rwytew45.jpg', '123.jpg',
'123.gif', 'test4.jpg', 'nodigits.jpg');
foreach ($files as $f) {
print preg_replace('/(\w\D)(\d\.jpg)/', '${1}0$2', $f);
print "\n";
}
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Paul 'piz' Wellner Bou
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Regular Expressions


Another way which should work, not including the file extension.
(The original extension will be kept)

preg_replace("/([^0-9])([0-9]{1})\./", "\${1}0$2.", $val);

Greetz,
Paul.
Shawn Wilson
Guest
 
Posts: n/a
#6: Jul 17 '05

re: Regular Expressions


Shmuel wrote:[color=blue]
>
> It didn't work until I made it like this:
> print preg_replace("|^([^\d]+)(\d)(\.jpg)$|", "\${1}0\$2\$3", $str) .
> "<br />";[/color]

My mistake :(

Shawn


--
Shawn Wilson
shawn@glassgiant.com
http://www.glassgiant.com
Closed Thread