This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
remiX's profile picture

string.gmatch( pattern ) over a string with multiple lines

Started by remiX, 12 March 2013 - 06:18 AM
remiX #1
Posted 12 March 2013 - 07:18 AM
Is it possible to use string.gmatch( p ) for text like this?

<!-- ad -->
                   <!--cached-tue 11 mar-->I need text here
<!-- ad -->

(Yeah second line has indentation)

I've tried this:

content = [[<!-- ad -->
                   <!--cached-tue 11 mar-->I need text here
<!-- ad -->]]


for _, text in content:gmatch(
[[<!-- google_ad_section_start -->
                   <!--cached-(.+)-->(.-)
<!-- google_ad_section_end -->]]
) do
    print( text )
end

But it prints nothing
Bubba #2
Posted 12 March 2013 - 07:47 AM
It's because you're using "magic" characters such as ! and -. Escape these using %. Example:

local content = [[<!-- ad -->
  <!--cached-tue 11 mar-->TEXT!
<!-- ad -->]]

for i in content:gmatch([[<%!%-%-cached..-%-%->(.-)<%!%-%-]]) do
  print(i)
end

And another thing, you're using a for i,v in content:gmatch() when gmatch will only return one value. v would be nil in this case.
remiX #3
Posted 12 March 2013 - 07:51 AM
It's because you're using "magic" characters such as ! and -. Escape these using %. Example:

local content = [[<!-- ad -->
  <!--cached-tue 11 mar-->TEXT!
<!-- ad -->]]

for i in content:gmatch([[&amp;lt;%!%-%-cached..-%-%-&amp;gt;(.-)&amp;lt;%!%-%-]]) do
  print(i)
end

And another thing, you're using a for i,v in content:gmatch() when gmatch will only return one value. v would be nil in this case.

Magic character? Never heard of this before lol will test it..

The reason for that was because the date 'tue 11 mar' is also unknown and will be different - how can i escape that?

edit: oh i see the double dots does that.
Thanks :P/>