[TxMt] help with perl search/replace for paragraphs

Oliver Taylor oliver at ollieman.net
Thu Sep 14 18:02:19 UTC 2006


I'm using a perl search/replace string to re-format documents into  
HTML. I'm using the following for regular paragraphs:

s/^[^\n\t\<].*/<p>$&<\/p>/g;

That takes care of single-line paragraphs like this:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do  
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim  
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut  
aliquip ex ea commodo consequat.

This is just like what you'd get from Markdown, paragraphs separated  
by a blank line are wrapped in a paragraph tag. But I'd like to add a  
rule that looks for paragraphs that have hard-breaks in them, like this:

Lorem ipsum dolor sit amet,
consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore
et dolore magna aliqua.

and wrap them in markup like this:

<p>Lorem ipsum dolor sit amet, <br />
consectetur adipisicing elit, sed do <br />
eiusmod tempor incididunt ut labore <br />
et dolore magna aliqua.</p>

The key is finding lines that end only to be followed by more lines  
in the same paragraph. My RegEx-fu is okay, but not great, so I've  
come close, but I can't get it right.

Thanks in advance.



More information about the textmate mailing list