Convert first lowercase to uppercase and uppercase to lowercase (regex?)

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-15 06:14:09

问题


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!


回答1:


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:

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.




回答2:


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.



来源:https://stackoverflow.com/questions/53770261/convert-first-lowercase-to-uppercase-and-uppercase-to-lowercase-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!