代码之家  ›  专栏  ›  技术社区  ›  eams

标记后的第一个字母大写

  •  2
  • eams  · 技术社区  · 9 年前

    我正试图想出一种搜索和替换方法来将标签后的第一个字母大写,但我没有运气。

    我正在使用正则表达式模式 Notepad++ .

    2 回复  |  直到 9 年前
        1
  •  6
  •   jbaums    9 年前

    在Notepad++中,在“查找和替换”对话框中启用正则表达式模式,然后查找:

    (?<=<p>)(.)
    

    并替换为:

    \U\1
    

    要解释要匹配的模式:

    (?<=a)b   # A positive lookbehind, i.e. match all b that immediately follow a, but
              #   don't match the "a" itself.
    (.)       # Find any character (i.e. "."), and capture it.
    (?<=a)(.) # Find any character that immediately follows a, and capture it.
    

    更换:

    \1   # The first captured substring in each match.
    \Ux  # Convert x to upper case.
    \U\1 # Convert the first captured substring in each match to upper case.
    

    请注意,这将尝试转换第一个 性格 至大写。如果 <p> 以及你想要大写的字母,你可以这样做:

    (?<=<p>)([^A-Za-z]*)(.) 
    
    # [^x]       Matches any character that is not x.
    # [^A-Za-z]  Matches any character that is not one of the upper case 
    #              or lower case letters.
    # x*         Matches zero or more consecutive x.
    # [^A-Za-z]* Matches zero or more consecutive characters that are not
    #              upper case or lower case letters.
    

    并替换为

    \1\U\2 # The first captured substring (any non-letter characters
           #   that immediately follow <p>) followed by the second captured
           #   substring (the first letter that appears after <p>), which 
           #   is converted to upper case.
    

    要找到的模式是:“匹配(和捕获,在捕获组1中)紧跟其后的任何非字母字符 <p> ,然后匹配(和捕获,在捕获组2中)紧跟在非字母字符之后的第一个字符(当然,必须是我们希望确保为大写的字母) * ,当后面没有非字母字符时,也会产生匹配 <p> ,在这种情况下,捕获组1将只保存一个空字符串。

        2
  •  1
  •   Dimitris Karagiannis    9 年前
    /<p>\s*(.){1}/
    

    这将与 <p> 标记后跟任何类型的空格0次或更多次,后跟任何字符1次,它将记住1个字符,以便以后使用它将其转换为大写。