Rare substitution in filename


#1

Hi people,

Manipulating RegEx, I had come into one that I don't know how to solve: I need to remove '-' and make some substitutions into a file name, I've done it using the format value with the $right, $left and so, but I'd like to do it using the RegEx, as it suits more my purposes, here comes the issue:

Original Filename:
123-456 A file to change version 123.123 201145-63 (2025-36-87-833)

Final Filename:
123-456 A file to change version 123.123 201145-63 (-253687833)

My capture expression is:
^(.+)\s((\d\d\d\d-\d\d-\d\d-\d\d\d))$

But my replace:
$1 (-$2$3$4$5)

gives:
123-456 A file to change version 123.123 201145-63 (-2025-36-87-833$3$4$5)

From this, I see that only spaces serves as delimiter, no way to definite others ???

I don't know how to make disappear the '-' between parenthesis, and at the same time substitute the first two digit into with a '-'... and obviously let the rest of filename untouched ??

Thanks for your patience.


#2

First of all you have a ^ anchor to your regexp which there is no call. Tip is to start at the back and work your way backwards where you need to concentrate your attention. And also use the feature to make sure your doing it right. I know it doesn't have the _FILENAME but you can substitute it for something else for the time being.


Here's something that works:

Begin Action Group _Script Test#TEST

Action #1 Actiontype 5: Format value Field ______: _FILENAME Formatstring: $regexp(%_filename%,'(.+\()\d{2}(\d{2})-(\d+)-(\d+)-(\d+\))$,'$1-$2$3$4$5')

End Action Group _Script Test#TEST (1 Action)

Study this to see how the regexp works and if you have any questions please ask





#3
$regexp(%_FILENAME%,'^(.+)\s\(\d\d(\d\d)-(\d\d)-(\d\d)-(\d\d\d)\)$','$1 (-$2$3$4$5)')

DD.20121110.0451.CET


#4

Woow, that's it. It works fine, so the secret was in the parenthesis.
Thank you stevehero and DetlevD


#5

Excuse me Stevehero, can you explain me a little bit the related with the caret anchor. I was reading the related article in http://www.regular-expressions.info/anchors.html and wikipedia, but didn't find my way out with the "Tip" you suggest me... How can I start from the back ? and, the ^ and $ weren't necessary to definite a string where we are working ??? I take notice that none of the solution you give me was using those anchors !!!

Thanks


#6

With an expression like this you can use the ^ anchor because you want to match the start of the string and the end also.

Begin Action Group _Script Test#TEST

Action #1 Actiontype 5: Format value Field ______: _FILENAME Formatstring: $regexp(%_filename%,'^(\d{3}-\d{3}.+\()\d{2}(\d{2})-(\d+)-(\d+)-(\d+\))$,'$1-$2$3$4$5')

End Action Group _Script Test#TEST (1 Action)

Will match:
123-456 A file to change version 123.123 201145-63 (2025-36-87-833)

Won't match:
A file to change version 123.123 201145-63 (2025-36-87-833)

See attached for breakdown of regexp.


When using .+ which means any character any number of times at the start of the regexp when using the $ anchor it is irrelevant is all I'm saying and is not required.

The solution I pasted in the 1st post messed up and has been doing that for some time now. I will edit it to suit. It seems to take out the $ sign.

Anyone else have this problem?



#7

It's odd, the solution of DetlevD use both ^ and $, yours stevehero, in the first post (just edited) only $ at the final, however I try also using none of them, and it works also, what a mess ?!! All three possibilities do the job !!! I can't see the role !


#8

However, something like:

$regexp(%_filename%,'(\d{3}-\d{3})^(.+()\d{2}(\d{2})-(\d+)-(\d+)-(\d+))$,'$1-$2$3$4$5')

didn't work to change:
123-456 A file to change version 123.123 201145-63 (2025-36-87-833)

into:
A file to change version 123.123 201145-63 (-253687833)


#9

I always try to describe a regex mask as complete as possible, that gives more reliability and robustness against outliers in the abundance of the many possible different input data.

DD.20121111.1800.CET


#10

Your example is bad regex syntax.

This should work ...
From:
123-456 A file to change version 123.123 201145-63 (2025-36-87-833)
To:
A file to change version 123.123 201145-63 (-253687833)

$regexp(%TEST%,'^\d\d\d-\d\d\d\s(.+)\s\(\d\d(\d\d)-(\d\d)-(\d\d)-(\d\d\d)\)$','$1 (-$2$3$4$5)')
                            <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->~~~~        ~~~~~~ ~~~~~~ ~~~~~~ ~~~~~~~~<!--colorc--></span><!--/colorc-->
                            <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$1         <!--colorc--></span><!--/colorc--> <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$2    <!--colorc--></span><!--/colorc--> <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$3    <!--colorc--></span><!--/colorc--> <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$4    <!--colorc--></span><!--/colorc--> <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$5<!--colorc--></span><!--/colorc--></b><!--fontc--></span><!--/fontc--><!--sizec--></span><!--/sizec-->

DD.20121111.1841.CET


#11

Thanks DetlevD,

The three suggestions works finely, but when I tried:

$regexp(%_filename%,'(\d{3}-\d{3})^(.+()\d{2}(\d{2})-(\d+)-(\d+)-(\d+))$,'$1-$2$3$4$5')

it was using the fact that ^ defines the start of a string so with this I was hoping to change:

123-456 A file to change version 123.123 201145-63 (2025-36-87-833)

into:

A file to change version 123.123 201145-63 (-253687833)

So I considered that all the job will concentrate only on the string between ^ and $, am I right ? It's to understand the functionalities of those two anchors. Continuing the learning process...


#12

You misunderstood... it will only match not change anything... We are only concentrating on the '^(\d{3}-\d{3}.+()\d{2}(\d{2})-(\d+)-(\d+)-(\d+)) part of the $regexp for the moment.

Considering ^\d{3}-\d{3} is added to the script to insure proper matching at the start of the script. The ( is now moved from its previous position at .+ to the start to capture the beginning of the text string thus leave the exact same $1-$2$3$4$5 in the replace part of the $regexp

But if I understand correctly you want to change:
123-456 A file to change version 123.123 201145-63 (2025-36-87-833)

into:
A file to change version 123.123 201145-63 (-253687833)

Begin Action Group _Script Test#TEST

Action #1 Actiontype 5: Format value Field ______: _FILENAME Formatstring: $regexp(%_filename%,'^\d{3}-\d{3}\s+(.+\()\d{2}(\d{2})-(\d+)-(\d+)-(\d+\))$,'$1-$2$3$4$5')

End Action Group _Script Test#TEST (1 Action)

Screen-shot of regexp breakdown




#13

So, am I right when I say that only the parenthesis mark the $1, $2,... parts and ^ and $ mark the start and end of a string, and had no functionality on the action of a regexp ???
So, if I have a text with, say, three phrases, and I want to work on the second phrase, I must put ^ and $ at start and end of text, but do the job only on $2, $3, $4, $5, considering that $1 and $6 defines the 1st and 3rd phrases ???

Your suggestion works, by the way, stevehero :laughing:


#14

Read there about anchors ...
http://www.regular-expressions.info/anchors.html

Hm, how to say it in other words?
Maybe ...
... without any anchor: floating.
... with anchors: lashed.

Without any anchor a regex is allowed to float over a given string and can try to match at any position where ever possible.
A 'floating' regex can create a list of reoccurring matches.
A 'lashed' regex is only allowed to work at a fixed position or in a destinated area, giving sometimes a predictable number of matches.

I beg your pardon, regex specialists in the world, please forgive me for my flowery description.

DD.20121111.1907.CET


#15

So DetlevD, if I am right, and continuing with the same 'modified' example, a filename like:

123-456 A file to change version 123.123 201145-63 (2025-36-87-833) 123-456 A file to change version 123.123 201145-63 (2025-36-87-833).mp3

with anchors, must give:

123-456 A file to change version 123.123 201145-63 (2025-36-87-833) 123-456 A file to change version 123.123 201145-63 (-253687833).mp3

and without, must give:

123-456 A file to change version 123.123 201145-63 (-253687833) 123-456 A file to change version 123.123 201145-63 (-253687833).mp3 (it isn't the case, I get the same as with anchors)

Or it's just I fantasying ?? :w00t:


#16

Of course ^ and $ have functionality but your original post didn't require a ^ because there was an anchor $ at the end and with a .+ at the start to capture all the characters anyway which meant the ^ had no relevance when using the .+ because it was going to go back to the start regardless of it there or not.

On the other hand (couple of post on) you wanted 3 digits a - and 3 more digits and whitespace matched at the start of the string.

So it only made sense to anchor the start also like: ^\d{3}-\d{3}\s+

If you study the attached image in my previous post you can see exactly how the captures work. They are highlighted green in the left and top window.


#17

There are are some links regarding regular expressions ...

Einstieg in die Welt der Regulären Ausdrücke siehe dort ...
http://zmievski.org/c/dl.php?file=talks/co...egex-clinic.pdf

Wikipedia ...
http://de.wikipedia.org/wiki/Regul%C3%A4rer_Ausdruck

Regular Expression Basic Syntax Reference ...
http://www.regular-expressions.info/reference.html

Regular Expression Tutorial ...
http://www.regular-expressions.info/tutorial.html

Mp3tag Manual ...
http://help.mp3tag.de/options_format.html#regexp

Mp3tag Examples ...
/t/521/1

DD.20121111.2125.CET


#18

Hi folks,

Well, it's just to thank you both stevehero and DetlevD for the help, now after studying the present issue with its ramifications, and the references of the links by DetlevD, I feel a little (just a little :flushed: ) less confused... But I assume that the regex is a huge world, and hope that with time and kind orientation from you, can get adequate knowledge...

Still to solve, the last issue, that is to convert:

"123-456 A file to change version 123.123 201145-63 (2025-36-87-833) 123-456 A file to change version 123.123 201145-63 (2025-36-87-833).mp3"

into:

"123-456 A file to change version 123.123 201145-63 (-253687833) 123-456 A file to change version 123.123 201145-63 (-253687833).mp3"

using only regex, anchors and parenthesis.

I'm on my way to solve it, if I have the chance to get it, I'll post it...

Until then, thank you again for your attention.

Greetings

N.B: Actually, this last substitution isn't the original issue, as this one is already solved, but I modified it to better understand the regex.


#19

You don't need to find the 'mp3' at the end seeing as I'm using the %_filename% and not the %_filename_ext%

Begin Action Group _Script Test#TEST

Action #1 Actiontype 5: Format value Field ______: _FILENAME Formatstring: $regexp(%_filename%,'^([^\(]+\()\d{2}(\d{2})-(\d+)-(\d+)-(\d+\))([^\(]+\()\d{2}(\d{2})-(\d+)-(\d+)-(\d+\)) ,'$1-$2$3$4$5$6-$7$8$9$10')

End Action Group _Script Test#TEST (1 Action)

See how regexp works:





#20
$regexp(%TEST%,'^(?:(\d{3}-\d{3}\s.+\s)\(\d{2}(\d{2})-(\d{2})-(\d{2})-(\d{3})\)\s*){2}$','$1(-$2$3$4$5)')
                 <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->~~~~~~~~~~~~~~~~~         ~~~~~   ~~~~~   ~~~~~   ~~~~~<!--colorc--></span><!--/colorc-->
                 <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$1                       <!--colorc--></span><!--/colorc--> <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$2     <!--colorc--></span><!--/colorc--> <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$3     <!--colorc--></span><!--/colorc--> <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$4     <!--colorc--></span><!--/colorc--> <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$5<!--colorc--></span><!--/colorc--></b><!--fontc--></span><!--/fontc--><!--sizec--></span><!--/sizec-->
$regexp(%TEST%,'^(?:([^(]+)\(\d{2}(\d{2})-(\d{2})-(\d{2})-(\d{3})\)\s*){2}$','$1(-$2$3$4$5)')
                 <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->~~~~~         ~~~~~   ~~~~~   ~~~~~   ~~~~~<!--colorc--></span><!--/colorc-->
                 <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$1           <!--colorc--></span><!--/colorc--> <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$2     <!--colorc--></span><!--/colorc--> <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$3     <!--colorc--></span><!--/colorc--> <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$4     <!--colorc--></span><!--/colorc--> <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$5<!--colorc--></span><!--/colorc--></b><!--fontc--></span><!--/fontc--><!--sizec--></span><!--/sizec-->
$regexp(%TEST%,'^(?:([^(]+)\(\d{2}(\d{2})-(\d+)-(\d+)-(\d+)\)\s*){2}$','$1(-$2$3$4$5)')
                 <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->~~~~~         ~~~~~   ~~~   ~~~   ~~~<!--colorc--></span><!--/colorc-->
                 <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$1           <!--colorc--></span><!--/colorc--> <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$2     <!--colorc--></span><!--/colorc--> <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$3   <!--colorc--></span><!--/colorc--> <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$4   <!--colorc--></span><!--/colorc--> <!--coloro:#000000--><span style="color:#000000"><!--/coloro-->$5<!--colorc--></span><!--/colorc--></b><!--fontc--></span><!--/fontc--><!--sizec--></span><!--/sizec-->

From:
123-456 A file to change version 123.123 201145-63 (2025-36-87-833) 123-456 A file to change version 123.123 201145-63 (2025-36-87-833)
To:
123-456 A file to change version 123.123 201145-63 (-253687833)

$regexp(%TEST%,'^(?:(\d{3}-\d{3}\s.+\s)\(\d{2}(\d{2})-(\d{2})-(\d{2})-(\d{3})\)\s*){2}$','$1(-$2$3$4$5) $1(-$2$3$4$5)') $regexp(%TEST%,'^(?:([^(]+)\(\d{2}(\d{2})-(\d{2})-(\d{2})-(\d{3})\)\s*){2}$','$1(-$2$3$4$5) $1(-$2$3$4$5)')

From:
123-456 A file to change version 123.123 201145-63 (2025-36-87-833) 123-456 A file to change version 123.123 201145-63 (2025-36-87-833)
To:
123-456 A file to change version 123.123 201145-63 (-253687833) 123-456 A file to change version 123.123 201145-63 (-253687833)

DD.20121113.0900.CET