The other answers provide good solutions to actually stripping lines from a string, but don't address why your code is failing to do that.
Reformatting for clarity, you wrote:
for line in result:gmatch"<Table [^\n]*" do
line = ""
end
The first part is a reasonable way to iterate over result
and extract all spans of text that begin with <Table
and continue up to but not including the next newline character. The iterator returned by gmatch
returns a copy of the matching text on each call, and the local variable line
holds that copy for the body of the for
loop.
Since the matching text is copied to line
, changes made to line
are not and cannot modifying the actual text stored in result
.
This is due to a more fundamental property of Lua strings. All strings in Lua are immutable. Once stored, they cannot be changed. Variables holding strings are actually holding a pointer into the internal table of reference counted immutable strings, which permits only two operations: internalization of a new string, and deletion of an internalized string with no remaining references.
So any approach to editing the content of the string stored in result
is going to require the creation of an entirely new string. Where string.gmatch
provides an iteration over the content but cannot allow it to be changed, string.gsub
provides for creation of a new string where all text matching a pattern has been replaced by something new. But even string.gsub
is not changing the immutable source text; it is creating a new immutable string that is a copy of the old with substitutions made.
Using gsub
could be as simple as this:
result = result:gsub("<Table [^\n]*", "")
but that will disclose other defects in the pattern itself. First, and most obviously, nothing requires that the pattern match at only the beginning of the line. Second, the pattern does not include the newline, so it will leave the line present but empty.
All of that can be refined by careful and clever use of the pattern library. But it doesn't change the fact that you are starting with XML text and are not handling it with XML aware tools. In that case, any approach based on pattern matching or even regular expressions is likely to end in tears.