user2334436
user2334436

Reputation: 939

Wrapping paragraphs/new line in p tags

I have multiple text file and need to wrap each paragraph in p tags using regex.

i.e. before:

Paragraph 1

Paragraph 2

Paragraph 3

After

<p>Paragraph 1</p>

<p>Paragraph 2</p>

<p>Paragraph 3</p>

I tried different regexs from other questions but with no luck. The closest i got was to use find (.*?(\n)) and replace with <p>$1</p> but the output looks like this:

<p>parag1
</p><p>
</p><p>parag2
</p><p>
</p><p>parag3
</p><p>
</p><p>parag4

Any idea how can i fix this? Thanks.

Upvotes: 2

Views: 2489

Answers (2)

james jelo4kul
james jelo4kul

Reputation: 829

Try this

(\w+\s+\d+)

Then replace with

<p>$1</p>

See demo

Upvotes: 0

Oscar Mederos
Oscar Mederos

Reputation: 29813

You can use this regex:

(.+?)(\n|$)+

and replace it with:

<p>$1</p>\n\n

The problem with your regex is, that it is matching empty lines as well, because you're saying: "Match any character zero or more times, followed by a new line".

You also have to take in count the last paragraph, that might not end with a line break.

Upvotes: 4

Related Questions