5 posts were split to a new topic: Extract catalogue number from album field
Here is one to use only the last name of a composer
$regexp(%composer%,(.+)(?=\W),)
So "Johann Sebastian Bach" in the composer field could become " Bach" in the filename.
This is only true, if the composer name is filled with "first name last name". This regex doesn't give you the "last name", it give you the last word filled in composer.
So "Bach, Johann Sebastian" would result in Sebastian (last word of his first name).
Yes, that's right, the last word of the composer. And as I've got it, it includes the space before that last word as well, which isn't ideal. But as my composer fields are formatted as "First Middle Last" it worked for my purpose, which was to put the composer's last name in the filename. It would fail in the case there was a suffix like "Johann Stauss Jr."
A regex to grab the first word should be even simpler and could key on the presence of a comma.
4 posts were split to a new topic: Rename based on certain tag fields
To my (already not redactable) earlier post.
Be, aware, first check it out! Pay attention to multiple surnames; to paternal + maternal surnames (for example, in spanish: "paternal" y "maternal", or inversely); and languages with proper using (= family name first) of family names (for example, hungarian or japan).
Fixing/Renaming files with invalid(invisible) characters.
I came across some MP3's with invisible characters (like Ø ) in their name because of some codepage hickups. It's quite easy to fix with a simple regex search and replace.
Actions -> Replace with regular expression
Field: _ALL
regex: \xc3\x98
Replace matches with: Ø
\xc3 = hex c3 = Ã
\x98 = hex 98 = invisible control character (on my PC)
A full list with possible hickups can be found here:
UTF-8 Encoding Debugging Chart
A post was split to a new topic: Change backslash to forward slash in Genre field
Few tips can help reduce complexity of Regular Expressions and likely make them run faster (see point 5).
Mp3Tag uses Perl Regular Expressions.
-
$vs\in replacement section.
e.g. swap artist name. Mercury, Freddy --> Freddy Mercury
RegExp:(\w+),(\w+)
ReplaceWith:\2, \1
\2, \1is equivalent to$2, $1and${2} ${1}
The later is very useful if a fixed digit must be added after a group, e.g.${1}3adds digit 3 after group1.
\w+captures a series of ASCII characters. -
Named group.
Ever wondering creating a complex regex and then starting counting the groups (1,2,3)? What if you create or remove a group? use ?<...> to give a human readable name to your group. We create here two groups:firstandlast.
Above regexp would become:
RegExp:(?<last>\w+),(?<first>\w+)
ReplaceWith:$+{first} $+{last}
Note the additional+here,$+{first}which is not used in numbered groups$1,${1}. -
Reuse pattern.
Named group can be used for recurring patterns. Could make your regex more human readable.
if we create a group name for our first/last name pattern(?\<name>[^\s]+)we can reuse it.
RegExp:(?<last>(?<name>\w+)), (?<first>(?&name))
ReplaceWith:$+{first} $+{last}
Note that(?&name)only reuses the patternname. Their actual captured values can be anything. The three groupslast,nameandfirst. -
Reuse pattern relative position
RegExp:(?<last>(?<name>\w+)), (?<first>(?-2))
ReplaceWith:$+{first} $+{last}
((?-2))here goes two groups behind (the 1st is its wrapper and the 2nd is the groupname). -
Reuse value.
\k{name}
To capture same value use\k{name}, instead of(?&name). This is the similar to what we normally do with\1. -
LookBehind. Magic switch
\K.
Perl supports only fixed length look behind (this is a pattern we want to check before the current cursor) but it since some versions includes the magic switch\K.This switch tells the system to use only the part after this switch.
Example, delete trailing non-ASCII characters of our above pattern.
RegExp:(?<first>(?\w+))\s(?<last>(?&name))\K[^\w]+
ReplaceWith""
Anything before the\Kis not involved in the replacement part.
All above are 100% tested in:
-
Actions (
replace with regular expression)
Enter source tag (e.g. artist) in Field:.
Enter text RegExp into Regular Expression:.
Enter text ReplaceWith into Replaces Matches with:. -
Function
$regexp(Text,'Find','Replace')
Enter source tag (e.g. %artist%) in Text.
Enter text RegExp into Find.
Enter text ReplaceWith into Replace. -
WebSources
regexpreplace "Find" "Replace"
Enter text RegExp into Find.
Enter text ReplaceWith into Replace.
Happy coding & testing.
A post was split to a new topic: Example to remove track number from TITLE via Regexp
