string.gsub pattern question

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

string.gsub pattern question

Daniel Hertrich
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

Reply | Threaded
Open this post in threaded view
|

Re: string.gsub pattern question

Duncan Cross
On Tue, Sep 27, 2011 at 10:53 AM, Daniel Hertrich
<[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

RE: string.gsub pattern question

Thomas Buergel
> 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:

Or simpler, do a two-step replacement:

1. "<br>\n" -> "\n"
2. "\n" -> "<br>\n"

strOutput = strInput:gsub("<br>\n", "\n"):gsub("\n", "<br>\n")

This doesn't cover cases where you have trailing whitespace (before \n), though. If the input is hand-written, you might want to eat all the trailing whitespace, too:

strOutput = strInput:gsub("<br>%s*\n", "\n"):gsub("%s*\n", "<br>\n")


Test case:

s="asdf<br>  \n12  34  \nblablah"
print(s:gsub("<br>%s*\n", "\n"):gsub("%s*\n", "<br>\n"))



Reply | Threaded
Open this post in threaded view
|

Re: string.gsub pattern question

Daniel Hertrich
Hi all,

thanks fopr your ideas,

On Tue, Sep 27, 2011 at 12:40 PM, Thomas Buergel
<[hidden email]> wrote:
>> 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:
>
> Or simpler, do a two-step replacement:

I solved the issue another (even simpler) way, which even solves the
issue of different line break codings on different platforms.
Pity that I didn't think of that before ;-)


nLineLength = string.len(line)
if string.sub(line, nLineLength-3, nLineLength) ~= "<BR>" then
        line = line .. "<BR>"
end


But your ideas were necessary to lead me to this solution. So thanks a lot!

Daniel

Reply | Threaded
Open this post in threaded view
|

Re: string.gsub pattern question

Luiz Henrique de Figueiredo
> nLineLength = string.len(line)
> if string.sub(line, nLineLength-3, nLineLength) ~= "<BR>" then
> line = line .. "<BR>"
> end

You don't need nLineLength. Try
        if string.sub(line, -3) ~= "<BR>" then

Reply | Threaded
Open this post in threaded view
|

Re: string.gsub pattern question

Dirk Laurie
> > nLineLength = string.len(line)
> > if string.sub(line, nLineLength-3, nLineLength) ~= "<BR>" then
> > line = line .. "<BR>"
> > end
>
> You don't need nLineLength. Try
> if string.sub(line, -3) ~= "<BR>" then
>
And if that does not work,

    if string.sub(line,-4):upper() ~= "<BR>" then



Reply | Threaded
Open this post in threaded view
|

Re: string.gsub pattern question

Daniel Hertrich
On Tue, Sep 27, 2011 at 4:59 PM, Dirk Laurie <[hidden email]> wrote:
>> You don't need nLineLength. Try
>>       if string.sub(line, -3) ~= "<BR>" then
>>
> And if that does not work,
>
>    if string.sub(line,-4):upper() ~= "<BR>" then

Works like a charm, thanks a lot! :-)

Daniel