IPB

Welcome Guest ( Log In | Register )

> Notice!

Please take a minute to check our Frequently Asked Questions. Use Search to reveal possible related topics.

Also make sure you've read the Forum Guidelines before posting in this forum.

4 Pages V   1 2 3 > »   
Closed TopicStart new topic
> Regular Expressions, Examples of actions with Regular Expressions
Florian
post Oct 16 2003, 19:47
Post #1


Developer


Group: Admin
Posts: 7723
Joined: 12-December 01
From: Germany, Dresden
Member No.: 203
Mp3tag Version: 2.65



This thread collects some useful regular expressions.

If you created a regular expression which solves a common task, please post it here and give a short description what it is supposed to do.

These regular expressions can be used with the action type Replace with regular expression. Please look at FAQ: How do I create a new action? to learn more about actions in Mp3tag.

-> If you have problems with a regular expression, please open a separate topic.


--------------------
♫ If you like using Mp3tag please donate to support further development.

Go to the top of the page
 
+Quote Post
Florian
post Oct 16 2003, 19:52
Post #2


Developer


Group: Admin
Posts: 7723
Joined: 12-December 01
From: Germany, Dresden
Member No.: 203
Mp3tag Version: 2.65



Trim leading/trailing spaces

See FAQ: Trim leading/trailing spaces.


--------------------
♫ If you like using Mp3tag please donate to support further development.

Go to the top of the page
 
+Quote Post
areve
post Oct 16 2003, 20:42
Post #3


Member


Group: Full Members
Posts: 12
Joined: 6-October 03
From: Brüssel
Member No.: 485



Regular expression: ^\s*[0-9]+\s*-\s*
Replace with:

This one will remove the track-number (if followed by a dash) and white space at the beginning of a string
(for instance 01 - Come Together will become Come Together)


Regular expression: ^\s*[0-9]+\s+
Replace with:

This one will remove the track-number if followed by a white space
(for instance 01 Come Together will become Come Together)

This post has been edited by dano: Jun 19 2011, 19:19
Reason for edit: fixed the RE
Go to the top of the page
 
+Quote Post
areve
post Dec 4 2003, 20:05
Post #4


Member


Group: Full Members
Posts: 12
Joined: 6-October 03
From: Brüssel
Member No.: 485



Here is another one.
I don't think it will be useful as such to a lot of people, but with REGEXP, examples are never too many rolleyes.gif

My files were tagged as such:
name of the artist.2003AL-name of the album
(the 2003AL meaning: released in 2003, Album, Live, but it could be 1977A, or 1995IR)
Weird, I know tongue.gif
I wanted to change that to a simpler
2003-name of the album
Here is what I did:
Regular expression: .*\.([0-9]*).*-(.+)
Replace with: $1-$2
Go to the top of the page
 
+Quote Post
phoenixdarkdirk
post Apr 11 2004, 20:50
Post #5


Member


Group: Members
Posts: 1
Joined: 11-April 04
Member No.: 866



This regular expression fixes tracknumbers from iTunes in the [track]/[numtracks] format (like 3/12).

Regex:
CODE
^(\d+)/\d+


Replace with:
CODE
$1


This can be applied to every file and it will only correct ones that have the / in them. Good luck!

According to RevRagnarok's suggestions below, I've made the expression less greedy.

This post has been edited by dano: Sep 3 2006, 17:30
Go to the top of the page
 
+Quote Post
areve
post May 30 2004, 21:20
Post #6


Member


Group: Full Members
Posts: 12
Joined: 6-October 03
From: Brüssel
Member No.: 485



Another one, that switches first and last names (for instance, Jacques Brel will become Brel, Jacques):
Regular Expression: ^(.+)\s(.+)$
Replace with: $2, $1
Go to the top of the page
 
+Quote Post
mll
post Jul 14 2004, 10:27
Post #7


Member


Group: Full Members
Posts: 22
Joined: 14-July 04
Member No.: 1041
Mp3tag Version: 2.37h



Used to use this basic regexp :

Applied on TRACK, find ^(\d)$ and replace by 0$1

It adds a leading 0 to the track number. I was thinking about enhancing it to apply it only to 1-digit numbers... when I discovered Ctrl+K smile.gif :

Cheers,

MLL

According to RevRagnarok's suggestions below, I've made the expression less greedy

This post has been edited by Florian: Nov 18 2004, 14:31
Go to the top of the page
 
+Quote Post
ThurstonX
post Jul 31 2004, 23:34
Post #8


Member


Group: Members
Posts: 3
Joined: 16-July 04
Member No.: 1045



If you have a filename:
  • 01 Trackname
and want it to be
  • 01 - Trackname
create an Action with the following properties:
  • Field: _FILENAME
  • Regular Expression: ([0-9]+)\s
  • Replace matches with: $1 - $2

Edit: Topic merged by moderator.

This post has been edited by Sebastian_Mares: Aug 1 2004, 08:53
Go to the top of the page
 
+Quote Post
RevRagnarok
post Nov 18 2004, 12:23
Post #9


Member


Group: Full Members
Posts: 15
Joined: 18-November 04
Member No.: 1325
Mp3tag Version: 2.26



I've noticed a lot of these RegExs 'go overboard' in their matching. A key to good expressions are limiting (1) the false matches and (2) the how long the engine needs to analyze the string.

For example, my two favorite expressions I have posted here are both above on this page. However, they have very greedy matchers that can easily result in lost data.

phoenixdarkdirk's - why NOT limit the track 'numbers' to digits with \d+ ?
mll's - really went overboard when a simple ^(\d)$ would've done it.

I highly recommend this book if you are serious about using REs. Of course, I recommend trying to find it at a technical library, because if you look it over a little you may realize you were just kidding and save yourself the money.

Of course, when we are using them on a handful of MP3 files, it's no big deal. wink.gif It's when you are handling megabytes of text files that it really matters.

Edit 5 Apr 2013 - After over eight years, fixed the URL to my blog.

This post has been edited by RevRagnarok: Apr 5 2013, 10:36
Go to the top of the page
 
+Quote Post
Florian
post Nov 18 2004, 14:34
Post #10


Developer


Group: Admin
Posts: 7723
Joined: 12-December 01
From: Germany, Dresden
Member No.: 203
Mp3tag Version: 2.65



RevRagnarok,

QUOTE (RevRagnarok @ Nov 18 2004, 01:23 PM)
I've noticed a lot of these RegExs 'go overboard' in their matching. A key to good expressions are limiting (1) the false matches and (2) the how long the engine needs to analyze the string.

Thanks a lot for your suggestions. I've changed the expressions you've mentioned to be less greedy.

Best regards,
~ Florian


--------------------
♫ If you like using Mp3tag please donate to support further development.

Go to the top of the page
 
+Quote Post
nickless
post Mar 29 2005, 22:22
Post #11


Member


Group: Full Members
Posts: 383
Joined: 9-August 04
From: Germany
Member No.: 1114
Mp3tag Version: 2.49



This RegEx will convert abbreviations composed of single chars and points between (and/or behind it) to uppercase.
Single chars without points around remains lowercase, except if a "-"char and a space is before it. (Example: "Songname - A text")

CODE
RegEx: ( |\.|^)(\w)(?= |\.|$)
Replace with: $1$upper($2)
and
CODE
RegEx: ([^-])( \u )
Replace with: $1$lower($2)

Example:
CODE
a.b a Reg.eX in p.o.d p. diidi e.t. - a bad a.i
==>
A.B a Reg.eX in P.O.D P. diidi E.T. - A bad A.I

Both regular expressions should be executed one after another

Use
CODE
RegEx: ( \u )
Replace with: $lower($1)
instead of second RegEx to have chars after " - " lowercase too. (Example: "Songname - a text")

Regards
nickless

Edit: removed some unnecessary characters from RegEx

This post has been edited by nickless: Jul 30 2005, 15:42


--------------------
My Exports

/(bb|[^b]{2})/ that is the question...
user posted image - emerge the world
Go to the top of the page
 
+Quote Post
ThurstonX
post Oct 15 2005, 02:01
Post #12


Member


Group: Members
Posts: 3
Joined: 16-July 04
Member No.: 1045



Title Tag Conversion

This 2-step action does:

1. set Title tag to Filename
2. convert titles using, e.g.:
01 - Track Name
to
01. Track Name

First step:
Format Value
Field: TITLE
Formatstring: %_FILENAME%

Second step:
Set a new Replace with Regular Expression action
Field: TITLE
Regular expression:
^([0-9]+)\s*-\s*
Replace matches with:
$1. $2

I use the first format for all file names, but my iRiver displays the title tag, which looks better (and saves one character) using the latter. For me it's a 2-step process: Filename-to-Tag (%TITLE% only) and then convert Title tag. Assumes your filename is the way you want it, of course.

This post has been edited by ThurstonX: Oct 15 2005, 02:17
Go to the top of the page
 
+Quote Post
dano
post Dec 28 2005, 16:12
Post #13


Moderator


Group: Moderators
Posts: 5688
Joined: 4-September 03
From: Germany
Member No.: 201
Mp3tag Version: 2.65



Upper case for Roman numbers

Regular expression:
\b(?:M{0,3})(?:D?C{0,3}|C[DM])(?:L?X{0,3}|X[LC])(?:V?I{0,3}|I[VX])(?=(\.\s|\s|\)|$))
Replace matches with:
$upper($0)
[ ] case-sensitive comparison

This post has been edited by dano: May 7 2008, 15:37


--------------------
Go to the top of the page
 
+Quote Post
Snykch
post Feb 19 2006, 15:35
Post #14


Member


Group: Members
Posts: 4
Joined: 16-February 06
Member No.: 2760
Mp3tag Version: 2.35



Convert artist names in the form 'Artist, The'

Regular expression: (.*),\sthe$
Replace with: The $1

This post has been edited by Snykch: Feb 19 2006, 15:43
Go to the top of the page
 
+Quote Post
Michaelm
post Apr 22 2006, 13:56
Post #15


Member


Group: Full Members
Posts: 7
Joined: 22-April 06
Member No.: 3051
Mp3tag Version: 2.35



And the other way around

Move "The " to the end

Regular expression: ^The (.+)
Replace with: $1, The


So "The Cure" becomes "Cure, The"
And "The a whole lot of words" becomes "a whole lot of words, The"
Go to the top of the page
 
+Quote Post

4 Pages V   1 2 3 > » 
Closed TopicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 23rd October 2014 - 04:48