Regular Expressions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • samug

    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 ?
  • Shawn Wilson

    #2
    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@glassgian t.com

    Comment

    • Shmuel

      #3
      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.jp g
      >>
      >>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.jp g => 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]

      Comment

      • Pedro Graca

        #4
        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.jp g', '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 =--

        Comment

        • Paul 'piz' Wellner Bou

          #5
          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.

          Comment

          • Shawn Wilson

            #6
            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@glassgian t.com

            Comment

            Working...