2

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

eams
  • 43
  • 5
  • EDIT: I have already searched through the provided answers on the site and they were either (a) unhelpful or (b)irrelevant with regards to my question. – eams Mar 14 '15 at 00:59
  • You could brute force it. Replace "

    a" with "

    A" and so on.

    – shawnt00 Mar 14 '15 at 01:32
  • Did you see this one? http://stackoverflow.com/questions/1039226/regex-to-change-to-sentence-case – shawnt00 Mar 14 '15 at 01:35
  • @shawnt00 I also tried using brute force as you put it but that didn't work either. And I saw that thread but the expression used by that person to match their text doesn't work for me. – eams Mar 14 '15 at 01:58
  • You were probably having problems with the angle brackets because you still had regular expressions selected. – shawnt00 Mar 14 '15 at 06:23

2 Answers2

6

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.

jbaums
  • 27,115
  • 5
  • 79
  • 119
1
/<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.

Dimitris Karagiannis
  • 8,942
  • 8
  • 38
  • 63
  • Hmm so I tried to use this to match to my text but it's not working. It is isn't matching my text. – eams Mar 14 '15 at 01:52
  • So here's my example text

    my dog died yesterday

    I was able to match using this ^

    . However, it's the replacement that's giving me problems. Perhaps I'm just inputting your answer wrong?

    – eams Mar 14 '15 at 01:54
  • Perhaps I'm doing this wrong but this isn't matching (finding) at all. I've input this answer in the "find what" section of the search/replace feature but it isn't able to find the "text". – eams Mar 14 '15 at 02:00
  • @eams ok, apparently notepad++ does some things differently in regards to regular expressions compared to javascript. I am sorry, I had not considered that. Know that the general idea of my regex above is sound, and it should work, provided it is written in a way n++ understands. See here, it may help you http://letconex.blogspot.gr/2013/06/how-to-use-regular-expressions-in.html – Dimitris Karagiannis Mar 14 '15 at 02:04
  • @Mitch Thanks for the link. I think my real problem is not knowing how to properly string together the expressions. – eams Mar 14 '15 at 02:09
  • @eams - sorry about your dog. – jbaums Mar 14 '15 at 03:45