Capitalizing the first letter after a tag

◇◆丶佛笑我妖孽 提交于 2020-01-02 11:11:54

问题


I'm trying to come up with a search and replace method for capitalizing the first letter after a tag, but I've had no luck.

I'm using the regex mode of Notepad++.


回答1:


In Notepad++, enable regex mode in the Find and Replace dialog box, then find:

(?<=<p>)(.)

and replace with:

\U\1

To explain the pattern to be matched:

(?<=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.

And the replacement:

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

Note that this attempts to convert the first character to upper case. If there might be other non-letter characters between the <p> and the letter that you want capitalised, you could the pattern:

(?<=<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.

and replace with

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

The pattern to be found says: "match (and capture, in capture group 1) any non-letter characters that immediately follow <p>, and then match (and capture, in capture group 2) the first character that immediately follows the non-letter characters (which must, of course, be the letter we want to ensure is upper case)". Note that because we use *, a match will also result when there are no non-letter characters following <p>, in which case capture group one will just hold an empty string.




回答2:


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

This will match a <p> tag followed by any kind of whitespace zero or more times, followed by any character 1 time and it will remember the 1 character so that you can use it later to turn it to uppercase.



来源:https://stackoverflow.com/questions/29044238/capitalizing-the-first-letter-after-a-tag

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