Reputation: 20464
Time ago I asked a question to format music filenames in certain conditions:
However, I noticed too late that the accepted answer is wrong, because it can capture any word starting with "F". But this is not a problem/question, I solved it just by restoring the ft|feat|featuring
OR group.
So finally from que question linked above, I ended up using this expression:
pattern := '^(.+)\s+-\s+(.+?)\s+(ft|feat|featuring)[\.\s]*([^([\])]+)(.+)?$'
replace := '$1 Feat. $4 - $2$5'
Well, now, having these filenames to test:
The expected results are these:
(from 1 to 4 no changes, and 16 is an assumable false positive, it is in essence the same as 5, 9 and 11.)
The expression that I mentioned works perfect for all the filenames except for the cases where the Feat... part is grouped inside parenthesis (or brackets, whatever).
Then I tried to adapt the RegEx for the aggrupations condition:
pattern := '^(.+)\s+-\s+(.+?)\s+([\[\(\{])?\s*(ft|feat|featuring([\.])?\s+)((.+)[^\]\)\}])?\s*(.+)?$'
But I ended messing my head and missing things, because it also captures the first parenthesis enclosure and the following characters till the end.
I need some help with this.
How I could fix/improve my expression to treat the mentioned filenames to get the expected results above?.
Or in other words, I need to maintain the "structure" of the expression but adding the capability to capture the Feat... part when it is inside parenthesis/brackets to properly format the filename.
PS: Please remember that I'm under pascal-script's RegEx syntax and their limitations (which I'm not sure about them).
I discovered that the author of the software that has this limitations it has support to run an external app from its pascal-script editor, so I can launch a CLI app written in .Net to perform the regex replacement, then I'm now under C#/Vb.Net RegEx motor improvements, nice!.
Upvotes: 3
Views: 107
Reputation: 43169
Something like:
^(?P<artist>.+?(?=\s-\s)) # artist with pos. lookahead
\s-\s # space - space
(?P<title>.+?(?=(?:\(?Feat\.)|$)) # title with pos. lookahead
\(? # optional open parenthesis
(?P<artist2>Feat\.[^()\n]+)? # artist2 with Feat. before
\)? # optional closing parenthesis
(?P<subtitle>.+)?$ # optional subtitle
See a demo on regex101.com.
Problem is that the dashes do not always match (maybe some additional programming logic?)
Upvotes: 2