Connecting Tech Pros Worldwide Help | Site Map

Regular Expressions

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 01:37 AM
samug
Guest
 
Posts: n/a
Default Regular Expressions

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 ?

  #2  
Old July 17th, 2005, 01:37 AM
Shawn Wilson
Guest
 
Posts: n/a
Default 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
  #3  
Old July 17th, 2005, 01:37 AM
Shmuel
Guest
 
Posts: n/a
Default 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]

  #4  
Old July 17th, 2005, 01:37 AM
Pedro Graca
Guest
 
Posts: n/a
Default 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 =--
  #5  
Old July 17th, 2005, 01:37 AM
Paul 'piz' Wellner Bou
Guest
 
Posts: n/a
Default 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.
  #6  
Old July 17th, 2005, 01:37 AM
Shawn Wilson
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.