#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Perl Regular Expression: 0XX, 00X, XXX


    hi, I would like to know if there is any regular expressions in Perl to search zeros at the start of a file name

    I want to then replace these zeros

    so

    00x.jpg > x.jpg
    0x0.jpg > x0.jpg
    xxx.jpg > xxx.jpg
    0xx.jpg > xx.jpg
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Um,
    Code:
    /^0/
    ??
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0
    doesn't work...
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    I love detailed error descriptions like that...

    So what "doesn't work"? What's your code? What's your test case?

    The regex I gave you matches a "0" at the beginning of the string. You still have to replace this match, of course.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by Jacques1
    I love detailed error descriptions like that...

    So what "doesn't work"? What's your code? What's your test case?

    The regex I gave you matches a "0" at the beginning of the string. You still have to replace this match, of course.
    Sorry, I know nothing about Perl.. anyway, I found what I needed by trial and error (but thank you anyway):

    (.*)-0*(.+)
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,930
    Rep Power
    1225
    Original requirement/request:
    is any regular expressions in Perl to search zeros at the start of a file name
    OP's solution:
    Originally Posted by dompter80
    Sorry, I know nothing about Perl.. anyway, I found what I needed by trial and error (but thank you anyway):

    (.*)-0*(.+)
    That regex pattern doesn't meet the requirements that you specified you needed. It matches a 0 anywhere within the filename where it is preceded by a dash and followed by anything. In fact it's less precise than that; since the 0 is optional, it only has to match a dash and one or more other characters.

    So, it would match this:

    --
    Last edited by FishMonger; February 13th, 2013 at 02:06 PM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    To remose all the 0's at the beginning of your string, try something like this:

    Code:
    $file_name =~ s/^0+//;
    Which is basically the regex Jacques gave you in the first place, which just the full replacement syntax.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by Laurent_R
    To remose all the 0's at the beginning of your string, try something like this:

    Code:
    $file_name =~ s/^0+//;
    Which is basically the regex Jacques gave you in the first place, which just the full replacement syntax.
    thanks guys

IMN logo majestic logo threadwatch logo seochat tools logo