Zero Padding Numbers, Episode 2

Hi again,

For another type of files mp3, I'm trying to zero pad them, but this time there's no '.' which I had taken as a reference in the first topic about this subject.

The files are like:

123456 English Class 67.mp3
654321 English Listening 23.mp3

I need to zero pad to 3 the number after Class, and to 2 the number after Listening, so I create an action group, which contains:

1-Format Value: A:
$if($neql($strstr(%_Filename%,lass),0),3,0)
(N.B: Here I put lass, because if I put class, it seems that there is sensibility to cases, and don't work, any suggestion welcomed)

2-Format Value: A:
$if($neql($strstr(%_Filename%,istening),0),2,0)
(N.B: Here I put istening, because if I put listening, it seems that there is sensibility to cases, and don't work, any suggestion welcomed)

3-Format Value: Filename
$if($eql(%A%,3),$regexp(%_Filename%,'^(\d+)\s([A-Za-z]+)\s([A-Za-z]+)\s([A-Za-z]+)\s(\d+)\s(-\d+)$','$1 $2 $3 $4 000$5 (-$6)'),%_Filename%)

4-Format Value: Filename
$if($eql(%A%,3),$regexp(%_Filename%,'^(\d+)\s([A-Za-z]+)\s([A-Za-z]+)\s([A-Za-z]+)\s(\d+)\s(-\d+)$','$1 $2 $3 $4 00$5 (-$6)'),%_Filename%)

BUT SURPRISE, AS YOU CAN IMAGINE, IT DIDN'T WORK, the 1 and 2 actions are OK, but I don't know what I'm doing wrong with the 3 and 4. I would like if you can correct me the error, but I'm opened to other ideas.

Any one can help? Thanks

For the case sensitivity problem, you can turn %_filename% to all upper or lower case for the comparison.

$strstr($lower(%_filename%),'class')

In regular expressions you can use ^ and $ as "anchors" to mean the start and end of the string, respectively. In your case you know the digits are always at the end, so using $ is the key and would function very much like the period in the last regular expression. The important part of what you want to match is a sequence consisting of a series of digits that follow a space, and are followed by the end of the string.

See: http://www.regular-expressions.info/anchors.html

Something like the following (I didn't test this):

'^(.*)\s(\d+)$'

You can turn on case insentive with: (?i)
You can turn off case insentive with: (?-i)

You can place these at different intervals of your reg exp or just have one at the start of your reg exp such as '(?i)' to not worry what case the text string is.

Padding zeros _FILENAME: EDIT: Read the OPs Problem wrong.

Action type: Format value
Field: _FILENAME
Formatstring: $regexp($regexp($regexp(%_filename%,'^(\d{6}.+)\s+([0-9]+)$','$1 000$2'),'(?i)^(\d{6}.+\s+listening\s+)\d+(\d{2})$','$1$2'),'(?i)^(\d{6}.+\s+class\s+)\d+(\d{3})$','$1$2')

Results:

123456 English Class 7

654321 English Listening 23
123456 English Class 00000000000006
123456 English class 07
654321 English listening 23
123456 English class 00000000000006

123456 English Class 007
654321 English Listening 23
123456 English Class 006
123456 English class 007
654321 English listening 23
123456 English class 006

If you want to change the padding at any stage just change the (\d{2}) or (\d{3}) where you see fit.

If you use regexp() there's an optional fourth parameter: 1 for case-insensitive, 0 for case-sensitive (default).

Another way to approach this would be to use Replace with regular expression actions. With this type of action there's a checkbox for case-sensitivity.

Action type: Replace with regular expression
Field: _FILENAME
Regular expression: ^(\d{6}\s+.+\s+Class)\s+(\d+).
Replace matches with: $1 $num($2,3).
[ ] case-sensitive comparison

Action type: Replace with regular expression
Field: _FILENAME
Regular expression: ^(\d{6}\s+.+\s+Listening)\s+(\d+).
Replace matches with: $1 $num($2,2).
[ ] case-sensitive comparison

_FILENAME in this context includes the file extension, so you can use the period following the digits instead of $ to anchor the expression. The file extension itself will remain unchanged.

Case 1
Action: Format value
Field : _FILENAME
Format string: $regexp($regexp(%_filename%,'^(\d+.+\sclass\s)(\d+)$','\1000\2',1),'^(.+?)0*(\d{3})$','\1\2')
Preview
From:
123456 English Class 7
123456 English Class 67
123456 English Class 167
To:
123456 English Class 007
123456 English Class 067
123456 English Class 167

Case 2
Action: Format value
Field : _FILENAME
Format string: $regexp($regexp(%_filename%,'^(\d+.+\slistening\s)(\d+)$','\1000\2',1),'^(.+?)0*(\d{2})$','\1\2')
Preview
From:
654321 English Listening 3.mp3
654321 English Listening 23.mp3
To:
654321 English Listening 03.mp3
654321 English Listening 23.mp3

DD.20121014.0919.CEST

Read the OPs post wrong. Changed post #3.

stevehero, DetlevD and JJ Johnson, thanks a lot to all of you, all your suggestions were different but functional, pretty than mine. I appreciate the trick to avoid to worry about cases. I erroneously imagined that with one $regexp, I can do the work, but it seems to need more than one, I'll try to study all suggestions, to better understand the construction of the regular expressions (examples like yours are the best way to clarify ideas). Always there is something new to learn.

Thank you again, and in case of doubts, I'll let you know...

Greetings

1 $regexp in mine pads 3 leading zero's (maximum number you require) and the other 2 fix them to have only 2 or 3 depending on your requirements, so as you can see that is 3 steps and the minimum amount of $regexp you can have.

I'll try break down the $regexp best I can:

$regexp($regexp($regexp(%_filename%,'^(\d{6}.+)\s+([0-9]+),'$1 000$2'),'(?i)^(\d{6}.+\s+listening\s+)\d+(\d{2}),'$1$2'),'(?i)^(\d{6}.+\s+class\s+)\d+(\d{3}),'$1$2') $regexp:

1st: $regexp(%_filename%,'^(\d{6}.+)\s+([0-9]+),'$1 000$2')
2nd: $regexp(,'(?i)^(\d{6}.+\s+listening\s+)\d+(\d{2}),'$1$2')
3rd: $regexp(,'(?i)^(\d{6}.+\s+class\s+)\d+(\d{3}),'$1$2')

Explanation: PART 01

$regexp(%_filename%, replaces the pattern specified by the regular expression expr
			in the string <!--fonto:Courier New--><span style="font-family:Courier New"><!--/fonto--><b><!--coloro:#0000FF--><span style="color:#0000FF"><!--/coloro-->%_filename%<!--colorc--></span><!--/colorc--></b><!--fontc--></span><!--/fontc--> by repl. The fourth optional parameter enables ignore case (1) or disables the ignore case setting (0). Please note that you have to escape comma and other special characters in expr.<!--fonto:Courier New--><span style="font-family:Courier New"><!--/fonto--><b> </b><!--fontc--></span><!--/fontc--><!--fonto:Courier New--><span style="font-family:Courier New"><!--/fonto--><b><!--coloro:#0000FF--><span style="color:#0000FF"><!--/coloro-->' <!--colorc--></span><!--/colorc--></b><!--fontc--></span><!--/fontc-->Wraps the regexp (needed in certain regexp to stop errors but I use it in all)<!--fonto:Courier New--><span style="font-family:Courier New"><!--/fonto--><b><!--coloro:#0000FF--><span style="color:#0000FF"><!--/coloro-->

^ Anchors to the start of string
(\d{6}.+)
Start capture #1 ( with exactly 6 digits \d{6} then any character any amount of times between 1/infinity .+ then end capture #1 )

\s+ White-space any amount of times + between 1/infinity

([0-9]+)

Start capture #2 ( with any digit from 0-9 [0-9] (same as '\d') any amount of times + between 1/infinity + then end capture #2 ) $ Anchors to the end of string ' Wraps the regexp , Separates the regexp with the replace

'

Wraps the regexp $1 000$2 Replaces with capture #1 whitespace 000#2 (This is where the 0's are padded)

'

Wraps the regexp ) Closes $regexp(

Before:
123456 English Class 67
654321 English Listening 3
After:

123456 English Class 00067
654321 English Listening 0003

Explanation: PART 02

$regexp( Start $regexp #2 , Use %_filename% from before

'

Wraps the regexp (?i) Match reminder of regexp with the options: case insensitive

^

Anchors to the start of string (\d{6}.+\s+listening\s+) Start capture #1 ( with exactly 6 digits (\d{6} then any character any amount of times between 1/infinity .+ white-space \s any amount of times + between 1/infinity and end capture #2 )

\d+

Any Digit \d any amount of times + between 1/infinity (\d{2}) Start capture #2 ( with exactly 6 digits (\d{6} then any character any amount of times between 1/infinity .+ white-space \s any amount of times + between 1/infinity $ Anchors to the end of string **Important the this is here as you need to anchor the regexp to the end for (\d{2}) to work properly ' Wraps the regexp , Separates the regexp with the replace ' Wraps the regexp

$1$2

Replaces with capture #1 then #2 **Notice that \d+ was outside a capture like (\d{2}) therefore not replacing it and leaving the result with 2 paddings. ' Wraps the regexp ) Closes $regexp( Before:

654321 English Listening 0003
After:

654321 English Listening 03

Explanation: PART 03
Much the same as PART 02 only class is changed and (\d{3}) because you want different rules to pad class with 3 numbers.

Before:

654321 English Class 0003
After:

654321 English Class 003

This could be done with a single Replace with regular expression action, but I've always favored using multiple simple actions. One reason being that they can be more easily understood when you go back to them months later. But also because by being more atomic, they can be easily rearranged, edited, removed, etc.

Action type: Replace with regular expression
Field: _FILENAME
Regular expression: ^(\d{6}\s+.+)\s+(Class|Listening)\s+(\d+).
Replace matches with: $1 $2 $num($3,$if($eql($lower($2),class),3,2)).
[ ] case-sensitive comparison

WOOOW, That's what I call a magisterial explanation, thank you stevehero, you clarify me a lot of concepts.
Yours JJ Johnson is also nice, what I intended to do in four actions, you was able to put in one.

Sincerely, Thanks to all of you...

Your disciple :book: