Add zero for numbers 1-9 to so all are are two digits

A swapping-ground for Regular Expression syntax

Add zero for numbers 1-9 to so all are are two digits

Postby Ptz » Thu Jan 10, 2008 12:59 pm

Hello i just discovered this great program and i was hoping someone could tell me how to add no matter where in the file name the 1-9 digits are a zero so file1.ext will be file01.ext or file 1 .ext will be file 01 .ext thank you
Ptz
 
Posts: 5
Joined: Thu Jan 10, 2008 10:14 am

Re: Add zero for numbers 1-9 to so all are are two digits

Postby Admin » Thu Jan 10, 2008 2:24 pm

Hi,

Can't you just do a simple search and replace, without using regular expressions?

Jim
Admin
Site Admin
 
Posts: 1896
Joined: Tue Mar 08, 2005 8:39 pm

Re: Add zero for numbers 1-9 to so all are are two digits

Postby Stefan » Thu Jan 10, 2008 2:55 pm

Without RegEx i mean you have to search and replace 9-times...


... so here a quick RegEx solution:
(note: if this works depends on your real file names, if you have gave bad examples....)

> name the 1-9 digits are a zero
> so file1.ext will be file01.ext
> or file 1 .ext will be file 01 .ext thank you

1.) think which part you can catch from the file name
all chars .+
an blank or not \s*
one digit \d

2.) so search for "all char/sign followed by an blank (or not) - followed by an single digit at the end"
(.+\s*)(\d)

3.) and replace with all from group 1, an new zero Ø, and the original digit from group 2
\10\2

HTH?
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: Add zero for numbers 1-9 to so all are are two digits

Postby Ptz » Fri Jan 11, 2008 3:16 am

Hi,

Can't you just do a simple search and replace, without using regular expressions?

Jim




Hello,


No, sorry they are varied file types, and I have been putting off doing the renaming manually for quite a while, so the number that I need to do is large. So, I was looking for a catch all solution.
Ptz
 
Posts: 5
Joined: Thu Jan 10, 2008 10:14 am

Re: Add zero for numbers 1-9 to so all are are two digits

Postby Ptz » Fri Jan 11, 2008 3:30 am

Without RegEx i mean you have to search and replace 9-times...


... so here a quick RegEx solution:
(note: if this works depends on your real file names, if you have gave bad examples....)

> name the 1-9 digits are a zero
> so file1.ext will be file01.ext
> or file 1 .ext will be file 01 .ext thank you

1.) think which part you can catch from the file name
all chars .+
an blank or not \s*
one digit \d

2.) so search for "all char/sign followed by an blank (or not) - followed by an single digit at the end"
(.+\s*)(\d)

3.) and replace with all from group 1, an new zero Ø, and the original digit from group 2
\10\2

HTH?





Hello ,

Stefan, it seems you are close, but your regular expression is adding an additional zero to all numbers, without regards if they are single or multiple digits. Only the single digits (1-9) need to have a zero preceding the number .
current regular expression does:
file11.ext becomes file011.ext
file003.ext becomes file0003.ext


Thank you
Ptz
 
Posts: 5
Joined: Thu Jan 10, 2008 10:14 am

Re: Add zero for numbers 1-9 to so all are are two digits

Postby Admin » Fri Jan 11, 2008 10:38 am

I think you'll need to check for a non-digit following a digit. \D goes partway towards this, e.g.

Code: Select all
(.+\s*)(\d)\D


but doesn't handle all situations.


Jim
Admin
Site Admin
 
Posts: 1896
Joined: Tue Mar 08, 2005 8:39 pm

Re: Add zero for numbers 1-9 to so all are are two digits

Postby Stefan » Fri Jan 11, 2008 9:52 pm

Search: (.+\D\s*)(\d)$
Replace: \1Ø\2

Code: Select all
GROUP 1
All char/sign               .+
till an non digit           \D
followed by an blank or not \s*


followed by GROUP 2
one digit                   \d


followed by nothing!!!
END of string               $
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: Add zero for numbers 1-9 to so all are are two digits

Postby Ptz » Sat Jan 12, 2008 2:28 am

Stefan wrote:Search: (.+\D\s*)(\d)$
Replace: \1Ø\2

Code: Select all
GROUP 1
All char/sign               .+
till an non digit           \D
followed by an blank or not \s*


followed by GROUP 2
one digit                   \d


followed by nothing!!!
END of string               $


Stefan, thank you. It works.
Ptz
 
Posts: 5
Joined: Thu Jan 10, 2008 10:14 am

Re: Add zero for numbers 1-9 to so all are are two digits

Postby Ptz » Sat Jan 12, 2008 2:30 am

Admin wrote:I think you'll need to check for a non-digit following a digit. \D goes partway towards this, e.g.

Code: Select all
(.+\s*)(\d)\D


but doesn't handle all situations.


Jim

Jim, thank you. For now, Stefan's solution works for what I need.
Ptz
 
Posts: 5
Joined: Thu Jan 10, 2008 10:14 am

Re: Add zero for numbers 1-9 to so all are are two digits

Postby bobkoure » Fri Jan 09, 2009 4:26 am

Looking for a non-digit followed by a digit followed by a non-digit doesn't handle beginning and end of line. Why not use lookahead and lookbehind to find all digits not preceded by a digit and not followed by a digit (which would mean that you've got a single digit.
from (?<!\d)(\d)(?!\d) to 0\1
bobkoure
 
Posts: 16
Joined: Mon Jul 10, 2006 5:38 pm


Return to Regular Expressions