1

I tried to count sentences in the text using the :s///gn command. It does no replace actually, just counts. The total command was:

:%s/\C[A-Z]\_.\{-}[.?!]\_s\+//gn

A sentence must start with a capital letter, then lazy \{-} takes as little as possible of any symbols including the newline, then a fullstop or one of ! or ? marks, then a space, including the newline. However, it works wrong if a sentence contains a newline and a TeX $-expression inside. For example,

Assume that a crew
 consists of $N+1$ pirates,
 one of them is the captain.

The result is TWO sentences! However, if we use instead

:%s/\C[A-Z]\_.\{-}[.?!]\_s\+/&/g

we get ONE sentence, as it must be. Also we have one match if we use /c. Without the TeX $-expression it work fine. If the sentence is within one line, it also works fine. However, something seems to be wrong in general. Vim is 7.4.

1
  • 2
    It is not so much the $-expression, but the uppercase letter within the expression (N). There are two matches: Assume ... . and N+1$ ... . But I do not know why there is only one match when Assume and N are on the same line.
    – husB
    Aug 12, 2022 at 15:51

1 Answer 1

0

It seems that %s/pattern//n add the matches count of pattern for every line even if the previous line match consume part of the current line.

Hence if your sentence contains uppercase on any subsequent line it is counted twice.

You'll get a better answer with:

\C\([.?!]\s*\|^\)\@<=[A-Z]\_.\{-}[.?!]\_s\+

The \([.?!]\s*\|^\)\@< match only if you are following a end of sentence (., ? or !) or if you are at the start of the line.

It will still fails for example like this:

The king
Henry IV was brave. 

But it should get much better result and in particular will address the example you mentioned.

2
  • 1
    Thank You for such a complete and useful answer. Aug 14, 2022 at 19:56
  • You are welcome ;-). If the answer works for you, you can vote for it and validate it :-) Aug 14, 2022 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.