'unable to splt string to go into a table

I am trying to split a string so certain lines go into a table in lua

str=[[  
[FW]

show 
[FW> show template-stack 
[FW> show template-stack | 
[FW> show template-stack | match 
[FW> show template-stack | match Stack:
[?1h=
Template Stack: NY_Stack
Template Stack: CT_Stack
Template Stack: DR_Stack

[K[?1l>FW> 
]]

this string is constantly updating but the values that I want to be put into a table all start with Template Stack:

Any idea on how I can put just those 3 lines into a table?

lua


Solution 1:[1]

One could write a small function based on the comment from @lhf:

function ExtractTemplates (Text)
  local Result = {}
  local Index  = 1
  for Match in Text:gmatch("Template Stack: (.-)\n") do
    Result[Index] = Match
    Index = Index + 1
  end
  return Result
end

The code works as expected:

> TemplateTable = ExtractTemplates(str)
> TemplateTable[1]
NY_Stack
> TemplateTable[2]
CT_Stack
> TemplateTable[3]
DR_Stack

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Robert