RegEx pattern to limit dashes in these circumstances

Scenario

I use third-party file renaming software that is written in Delphi and supports the pascal script: http://www.den4b.com/?x=products&product=renamer

The application allows you to use regular expressions to rename files. this means that if what I need to do with the file name cannot be performed using only one RegEx, then I could use different expressions at the same time or also pascal-script code to place the file name until I can format it correctly file name for the needs of this question or something else ...

Problem

I need to format the song file names like below in these names: β€œ ... with the artist’s image ” is to the right of the line, I need to match this and put it on the left side of the line.

  • Carbin and Sirmark - Sorry Feat. Sevener
  • Kristjan Cash Cash - Take Me Home Feat. Bebe Rexha (Revoke Remix)

To make this easy to understand, we could imagine the tokenization of the file name as follows:

[0]ARTIST   [1]DASH   [2]TRACK   [3]FEAT_ARTIST   [4]POSSIBLE_ADDITIONAL_INFO_INSIDE:()[]{}

Then, what I need to do with RegEx formats the file name in order to position the tokens in the following order:

[0]ARTIST   [3]FEAT_ARTIST   [1]DASH   [2]TRACK   [4]POSSIBLE_ADDITIONAL_INFO_INSIDE:()[]{}

I really do this with this RegEx:

\ A ([^ -]?) \ S- \ s * \ s ([([]) ((feet [\ s] | feat [\ s] | distinguishing [(.?) ... \ s]) [^ () {} []] *) ([)]])? (. +)? \ Z

Replace with

$ 1 $ 4 - $ 2 $ 7

, [0]ARTIST [2]TRACK , , , :

  • Dj E-nergy C-21 - Super-hero! feat Dj Ass-hole

, , , , - , , , isn ' t, , .

ingenuos, , , filename , , .

RegEx, , / , , [0]ARTIST [2]TRACK ?

... , , RegEx , 1 "... > " ? ( )

Regex , [1]DASH [3]FEAT_ARTIST, , ( )

, Regex - ([\-]){1}, , , , .


...

[3]FEAT_ARTIST, , [0]ARTIST [2]TRACK.

  • : ' Carbin and Sirmark - Feat. Sevener
  • To: ' Carbin and Sirmark Feat. Sevener - '

[3]FEAT_ARTIST, , [0]ARTIST [2]TRACK. [4]POSSIBLE_ADDITIONAL_INFO_INSIDE:()[]{}.

  • : ' Flight Facilities - Heart Attack Feat. (Snakehips Remix) '
  • To: ' Feather. - (Snakehips Remix) '

[3]FEAT_ARTIST, , [0]ARTIST [2]TRACK. [4]POSSIBLE_ADDITIONAL_INFO_INSIDE:()[]{}, .

  • : ' Flight Facilities - Heart Attack Feat. Owl Eyes [Remix] -
  • To: ' Feather. - [Remix] -

[0]ARTIST a [2]TRACK, [3]FEAT_ARTIST, .

  • : ' Fedde Le Grand - Cinematic'
  • To: ' Fedde Le Grand - Cinematic'

[0]ARTIST a [2]TRACK, [3]FEAT_ARTIST [1]DASH, .

  • : ' Fedde Le Grand Feat. Denny White - '
  • To: ' Fedde Le Grand Feat. Denny White - '

[0]ARTIST , , [0]ARTIST [2]TRACK, Regex , .

  • : ' - -
  • To: ' - -

[2]TRACK , , [0]ARTIST [2]TRACK, Regex , .

  • : ' - --
  • To: ' - -- feat someone

[0]ARTIST [2]TRACK , , , Regex , .

  • : ' Dj E-nergy C-21 - -! feat Dj Ass-hole '
  • To: ' Dj E-nergy C-21 - Super-hero! feat Dj Ass-hole '

[0]ARTIST [2]TRACK , [3]FEAT_ARTIST , .

  • : ' Dj E-nergy C-21 - Super-hero!'
  • To: ' Dj E-nergy C-21 - Super-hero!'

, , .

+4
4

:

^(.+)\s+-\s+(.+?)\s+[fF](t|eat(uring)?)?\.?([^([\])\n]+)(.+)?$

DEMO

: $1 Feat.$5 - $2$6

ReNamer Regex101, , - ( + - + ), artist - name, , .

^(.+)\s+-\s+ .+ , . , , -, - "" , , . :

  • Artist - name - track title feat. someone - ,
  • Artist name - track - title feat. someone - , .

(ft[.\s]|feat[.\s]|featuring[.\s]) [fF](t|eat(uring)?)?\.?, , ( ).

+ \s+ ( ), , , , .

+1

, , /, - , " " " ". , ( , ). , , \A([^-]?)\s-\s* \A(.?)\s-\s+...

+1

UltraEdit 22.10:

Carbin & Sirmark - Sorry Feat. Sevener
Kristjan Cash Cash - Take Me Home Feat. Bebe Rexha (Revoke Remix)
Dj E-nergy C-21 - My Super-hero track! feat Dj Ass-hole
Flight Facilities - Heart Attack Feat. Owl Eyes (Snakehips Remix)
Flight Facilities - Heart Attack Feat. Owl Eyes [Snake--hips Remix]
Fedde Le Grand - Cinematic
Fedde Le Grand Feat. Denny White - Cinematic
Artist-Name - Track Name feat someone
Artist Name - Track-Name feat someone
Dj E-nergy C-21 - My Super-hero track! feat Dj Ass-hole
Dj E-nergy C-21 - My Super-hero track!

Perl

^(.+) - (.+?) ((?:featuring|feat\.?|ft\.?) +(?:[^\r\n (\[{]| (?![(\[{]))+)

$1 $3 - $2

Carbin & Sirmark Feat. Sevener - Sorry
Kristjan Cash Cash Feat. Bebe Rexha - Take Me Home (Revoke Remix)
Dj E-nergy C-21 feat Dj Ass-hole - My Super-hero track!
Flight Facilities Feat. Owl Eyes - Heart Attack (Snakehips Remix)
Flight Facilities Feat. Owl Eyes - Heart Attack [Snake--hips Remix]
Fedde Le Grand - Cinematic
Fedde Le Grand Feat. Denny White - Cinematic
Artist-Name feat someone - Track Name
Artist Name feat someone - Track-Name
Dj E-nergy C-21 feat Dj Ass-hole - My Super-hero track!
Dj E-nergy C-21 - My Super-hero track!

, . UltraEdit Boost Perl.

, , , :

\A(.+) - (.+?) ((?:featuring|feat\.?|ft\.?) +(?:[^ (\[{]| (?![(\[{]))+)

:

$1 $3 - $2

:

^...
\A...

(.+) - ... , 1 ( ) space dash space ,  - , .

(.+?) ... , ( ) ...

(?:featuring|feat\.?|ft\.?) +... word featuring feat ft 1 .

(... .

(?:[^\r\n (\[{]| (?![(\[{]))+... ,

    • ( UE)

  • lookahead, ,

. , ( [ {, , , space space dash FEAT_ARTIST .

)... .


1: ( UltraEdit) :

^(.+) - (.+?) ((?:featuring|feat|ft)[ .]+(?:[^\r\n (\[{]| (?![(\[{]))+)

featuring., .


2: ( UltraEdit) :

^((?:.(?! - ))+.) - ((?:.(?! - ))+) ((?:featuring|feat|ft)[ .]+(?:[^\r\n (\[{]| (?![(\[{]))+)

, space dash space FEAT_ARTIST.

lookahead, space dash space. , space dash space, space dash space, .

+1

regex @ m.cekiera pascal- script, , :

// Formats an audio filename that has the "...featuring artist" part at the end of filename.
//------------------------------------------------------------------------------------------


// Pseudo-Example:
//
// From: [0]ARTIST_NAME  [1]DASH  [2]TRACK_TITLE  [3]FEAT_ARTIST  [4]POSSIBLE_ADDITIONAL_INFO_INSIDE:()[]{}
// To:   [0]ARTIST_NAME  [3]FEAT_ARTIST  [1]DASH  [2]TRACK_TITLE  [4]POSSIBLE_ADDITIONAL_INFO_INSIDE:()[]{}

// Real-Example:
//
// From: Carbin & Sirmark - Sorry Feat. Sevener.mp3
// To:   Carbin & Sirmark Feat. Sevener - Sorry.mp3

// Known limitations:
//
// β€’ If [0]ARTIST_NAME or [2]TRACK_TITLE parts contains any " - " the script will not work properlly.
//   By default the script prevents any replacement on that kind of filenames, so don't worry.


var
  rgxPattern: string;
  rgxReplace: string;
  dashCount: integer;
  baseName: string;
  extension: WideString;

begin

  baseName  := WideExtractBaseName(FileName)
  extension := WideExtractFileExt(FileName);

  // The regular expression that matches the filename parts.
  // http://stackoverflow.com/questions/32807698/regex-pattern-to-limit-dashes-in-these-circumstances
  rgxPattern := '^(.+)\s+-\s+(.+?)\s+[fF](t|eat(uring)?)?\.?([^([\])\n]+)(.+)?$'
  rgxReplace := '$1 Feat.$5 - $2$6'

  // The amount of " - " that contains the filename.
  dashCount := high(MatchesRegEx(baseName, '\s-\s' , false));

  // If only one " - " is found then...
  If (dashCount = 0) Then
    begin // Do the replacement.
      baseName := ReplaceRegEx(baseName, rgxPattern, rgxReplace, false, true)
      FileName := baseName + extension;
    end;

end.   
0

Source: https://habr.com/ru/post/1609172/


All Articles