Spyros_av
Spyros_av

Reputation: 893

Split text into paragraphs where there are "spaces" at the end of the text?

Using this (?sm)^.*?\.$ I retrieve every paragraph from my text.

When there are "spaces" at the end of the text the last paragraph isn't matched. What do I have to use to fix that?

Example text:

Copyright laws are changing all over the world. Be sure to check the
copyright laws for your country before downloading or redistributing
this or any other Project Gutenberg eBook.

This header should be the first thing seen when viewing this Project
Gutenberg file.  Please do not remove it.  

Please read the "legal small print", and other information about the
eBook and Project Gutenberg at the bottom of this file.    

regex101 example

Upvotes: 2

Views: 1635

Answers (2)

Federico Piazza
Federico Piazza

Reputation: 31035

If you want to fix your regex, then you can add \s* at the end of the pattern:

(?sm)^.*?\.\s*$

However, you can use a split method with a regex like this:

^\t*$

working demo

Upvotes: 2

Nikolas
Nikolas

Reputation: 44496

I use ((?:[^\n][\n]?)+) since I have worked on my school project. It captures you all the paragraphs separated with at least one line (\n).

It works simply: Just capture everything that is not a newline.

Check the Regex101 out.

Upvotes: 4

Related Questions