#1 2024-08-08 02:48

Maga999
Member
Registered: 2024-08-06
Posts: 5

Change the case before or after certain character

Hello!

I need help with regular expression code for this :-


The text before the following special characters in the file name  [-] OR [–] OR [—] >>> Capitalize Every Word

The text after the following special characters in the file name  [-] OR [–] OR [—] >>> Sentence case


For Example:


Input:

the last samurai – the way of the sword.mp3

The Result:

The Last Samurai – The way of the sword.mp3

Hope you help

Thanks!

smile

Offline

#2 2024-08-08 14:16

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,440

Re: Change the case before or after certain character

How about a Pascal Script rule instead?

var
  BaseName, Delimeter: WideString;
  DelimeterPosition: Integer;
  Positions: TIntegerArray;
  Matches: TWideStringArray;
begin
  BaseName := WideExtractBaseName(FileName);
  if FindRegEx(BaseName, '\s+(-|–|—)\s+', False, Positions, Matches) > 0 then
  begin
    Delimeter := Matches[0];
    DelimeterPosition := Positions[0];
    BaseName := WideCaseCapitalize(Copy(BaseName, 1, DelimeterPosition - 1)) + Delimeter +
      WideCaseSentence(Copy(BaseName, DelimeterPosition + Length(Delimeter), Length(BaseName)));
    FileName := BaseName + WideExtractFileExt(FileName);
  end;
end.

This script will handle all three delimiters that you mentioned: "-" (U+002D), "–" (U+2013) and "—" (U+2014).

Offline

#3 2024-08-08 17:48

Maga999
Member
Registered: 2024-08-06
Posts: 5

Re: Change the case before or after certain character

Works Perfectly!

Thank you so much smile

Offline

#4 2024-08-08 21:24

Maga999
Member
Registered: 2024-08-06
Posts: 5

Re: Change the case before or after certain character

I forget something..

If there's ( ) in the file name i want the text inside it to be sentence case also


Example:


Input:

the last samurai – the way of the sword (VERSION 01).mp3

The Result:

The Last Samurai – The way of the sword (Version 01).mp3


Can you edit the script please ?

Offline

#5 2024-08-10 22:35

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,440

Re: Change the case before or after certain character

This script will also handle the bracketed content at the end of the filename.

var
  BaseName, Match: WideString;
  Position: Integer;
  Positions: TIntegerArray;
  Matches: TWideStringArray;
begin
  BaseName := WideExtractBaseName(FileName);
  if FindRegEx(BaseName, '\s+(-|–|—)\s+', False, Positions, Matches) > 0 then
  begin
    Match := Matches[0];
    Position := Positions[0];
    BaseName := WideCaseCapitalize(Copy(BaseName, 1, Position - 1)) + Match +
      WideCaseSentence(Copy(BaseName, Position + Length(Match), Length(BaseName)));
    if FindRegEx(BaseName, '\(.*?\)\Z', False, Positions, Matches) > 0 then
    begin
      Match := Matches[0];
      Position := Positions[0];
      BaseName := Copy(BaseName, 1, Position - 1) +
        WideCaseSentence(Copy(BaseName, Position, Length(BaseName)));
    end;
    FileName := BaseName + WideExtractFileExt(FileName);
  end;
end.

Offline

#6 2024-08-11 17:43

Maga999
Member
Registered: 2024-08-06
Posts: 5

Re: Change the case before or after certain character

Excellent!

Thanks

smile

Offline

Board footer

Powered by FluxBB