[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: string.gsub pattern question
- From: Duncan Cross <duncan.cross@...>
- Date: Tue, 27 Sep 2011 11:14:57 +0100
On Tue, Sep 27, 2011 at 10:53 AM, Daniel Hertrich
<dhertrich@googlemail.com> wrote:
> Hi list,
>
> probably a trivial question to some of you, but I am stuck on this...
> I'd like to replace the endings of lines from a HTML-formatted string,
> using string.gsub(), according to the following criterion:
>
> Each line ending on "\n" only, but not on "<br>\n", shall be modified
> to end on "<br>\n"
> Or in words: All lines should end on "<br>\n", but some lack the
> "<br>", and if the "<br>" does not exist yet, generate it.
>
> Anyone can assist me with the pattern to use please?
>
> strung.gsub(strInput, ????, "<br>\n")
>
> Thank you!
> Daniel
>
>
I don't believe this can be done with a simple pattern replacement.
You have to move into using a custom callback function to do the
negative look-behind, something like:
string.gsub(strInput, "()\n", function(pos)
if (pos < 5) or (string.sub(strInput, pos-4, pos-1) ~= '<br>') then
return '<br>\n'
end
return '\n'
end)
-Duncan