lua-users home
lua-l archive

RE: string.gsub pattern question

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> 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"))

AltStyle によって変換されたページ (->オリジナル) /