将标签后的第一个字母大写

时间:2015-03-14 00:53:37

标签: regex capitalization

我正在尝试使用搜索和替换方法来标记标记后的第一个字母,但我没有运气。

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

2 个答案:

答案 0 :(得分:6)

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

要找到的模式说:&#34;匹配(并捕获,在捕获组1中)紧跟<p>之后的任何非字母字符,然后匹配(并在捕获组2中捕获)紧跟在非字母字符后面的第一个字符(当然,它必须是我们想要确保的字母是大写字母)&#34;。请注意,因为我们使用*,所以当<p>后面没有非字母字符时也会产生匹配,在这种情况下,捕获组1只会保留一个空字符串。

答案 1 :(得分:1)

/<p>\s*(.){1}/

这将匹配<p>标记,后跟任何类型的空白零次或多次,后跟任何字符1次,它将记住1个字符,以便您以后可以使用它将其转换为大写。