3

I have a huge file in this layout:

world12345:Betaalpha    
world12344:alphabeta    
world12343:ZetaBeta    
world12342:!betatheta

I need to convert the first lowercase letter after the ":" to uppercase and the first uppercase letter to lowercase. I've tried using notepad++ and emeditor, but I'm not that experienced with regex.

This is how I want it to become after (regex?)

world12345:betaalpha    
world12344:Alphabeta    
world12343:zetaBeta    
world12342:!betatheta   (unchanged, as the first char is a special char)

I have tried searching the web for a regex in npp+, but to no avail. Unfortunately, I'm not a scripter so I can't write one myself.

Thanks in advance!

dustinos3
  • 934
  • 3
  • 17
  • 27
marnix409
  • 33
  • 1
  • 4

2 Answers2

6

This is the simplest solution I could come up with.

Find what: :(\u)|:(\l)

Replace with: :\l($1)\u($2)

Enable the settings: Wrap Around & Match case

Search mode: Regular expression

Press Replace All.

Explanation

\u matches & converts uppercase, \l matches & converts lowercase.

enter image description here

J.A.P
  • 715
  • 6
  • 13
  • 1
    Nice, I wasn't aware of `\u,\l` for matching. – Toto Dec 14 '18 at 10:05
  • 1
    Thanks for the reply, tried your answer too. Also does the trick, thanks :) – marnix409 Dec 14 '18 at 12:52
  • @Toto it is Notepad++ specific regex. `\u` is a shorthand for `[A-Z]` and `\l` = `[a-z]`. So in other software one can just replace with that. – J.A.P Dec 14 '18 at 19:42
  • with your answer solved my problem easily, i had to lowercase after \n so I first replaced \n with % (I don't use % in my text) then used your example, then changed by % to \n... :D – Web.11 Feb 09 '20 at 19:45
2

Thanks to this answer, I was able to find a solution to your problem after initially thinking it wasn't possible.

The way to do this in Notepad++ is to use the following options:

  • Open the Replace dialog (Ctrl + H)
  • Find what: ^([^:]+:)(([A-Z])|([a-z]))([^:]+)$
  • Replace with: $1\L$3\E\U$4\E$5
  • Check Match case
  • Check Wrap around
  • Select Regular expression
  • Uncheck . matches newline
  • Press Replace All

Here's a GIF of this in action:

enter image description here

The breakdown of the Find what field:

  • ^ at the front of the Regular Expression represents the beginning of a line and $ at the end represents the end of a line. This prevents it from being lazy or wrapping to the next line.
  • ([^:]+:) represents the characters at the beginning of the line, allowing all characters except :. This is group $1
  • (([A-Z])|([a-z])) represents the first character after the :. If there is anything other than an upper or lowercase letter, it will skip the line.
    • Group $2 will be the first character, regardless of uppercase or lowercase. We'll ignore this in our replacement.
    • Group $3 will be the first character if it is uppercase, otherwise $3 will be empty.
    • Group $4 will be the first character if it is lowercase, otherwise $4 will be empty.
  • ([^:]+) represents the characters at the end of the line, allowing all characters except :. This is group $5.

The breakdown of the Replace with field:

  • $1 will be the first group as described above
  • \L$3\E will convert group $3 as described above to lowercase.
  • \U$4\E' will convert group$4` as described above to uppercase.
  • $5 will be the last group as described above

\L and \U stand for "beginning converting to lowercase" or "uppercase," respectively. \E stands for "stop converting." Since only one out of $3 or $4 will contain the first character (the other will be blank), this converts only in the case we want.

Worthwelle
  • 1,244
  • 1
  • 16
  • 19