且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将第一个小写转换为大写,将大写转换为小写(正则表达式?)

更新时间:2023-02-21 19:08:30

感谢此答案,我能够在最初认为不可能的情况下找到解决问题的方法.

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

在Notepad ++中执行此操作的方法是使用以下选项:

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

  • 打开替换"对话框( Ctrl + H )
  • 查找内容:^([^:]+:)(([A-Z])|([a-z]))([^:]+)$
  • 替换为:$1\L$3\E\U$4\E$5
  • 检查匹配情况
  • 检查环绕
  • 选择正则表达式
  • 取消选中.匹配换行符
  • 全部替换
  • 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

以下是此操作的GIF:

Here's a GIF of this in action:

查找内容字段的细分:

    正则表达式前面的
  • ^代表行的开头,结尾的$代表行的结尾.这样可以防止它变得懒惰或换行.
  • ([^:]+:)代表该行开头的字符,允许除:之外的所有字符.这是组$1
  • (([A-Z])|([a-z]))表示:之后的第一个字符.如果除了大写或小写字母外,其他任何内容都将跳过该行.
    • $2将是第一个字符,无论大小写如何.我们将在替代产品时忽略它.
    • $3如果是大写字母,将是第一个字符,否则$3将为空.
    • $4如果是小写字母,将是第一个字符,否则$4将为空.
    • ^ 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.

      替换为字段的明细:

      • $1将是如上所述的第一组
      • \L$3\E将如上所述将组$3转换为小写.
      • \U$4\E' will convert group $ 4`如上所述,为大写字母.
      • $5将是如上所述的最后一组
      • $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\U分别表示开始转换为小写"或大写". \E代表停止转换".由于$3$4中只有一个包含第一个字符(另一个将为空白),因此仅在需要时才进行转换.

      \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.