3

I can't figure out how to get Lua to return ALL matches for a particular pattern match.

I have the following regex which works and is so basic:

.*\n

This just splits a long string per line.

The equivelent of this in Lua is:

.-\n

If you run the above in a regex website against the following text it will find three matches (if using the global flag).

Hello
my name is
Someone

If you do not use the global flag it will return only the first match. This is the behaviour of LUA; it's as if it does not have a global switch and will only ever return the first match.

The exact code I have is:

local test = {string.match(string_variable_here, ".-\n")}

If I run it on the above test for example, test will be a table with only one item (the first row). I even tried using capture groups but the result is the same.

I cannot find a way to make it return all occurrences of a match, does anyone know if this is possible in LUA?

Thanks,

Mucker
  • 257
  • 3
  • 12
  • 1
    Use `gmatch`... – Wiktor Stribiżew Feb 17 '22 at 00:10
  • that worked. I couldn't find any mention of that function on the LUA doco about patterns. When I search for it though, it is there, just in a completely different section! The doco is all over the place for LUA. Do you want to post that as an answer and I'll flag it? – Mucker Feb 17 '22 at 00:23
  • 1
    Note: Consider using `io.lines` instead. This pattern will also only match lines with a trailing newline; consider using `gmatch"[^\n]+"` if you want to split by newline. – Luatic Feb 17 '22 at 07:57
  • bettrer use pattern `s:gmatch("[^%c]+")` - included \r\n and \n - all non-printable symbols – Mike V. Feb 17 '22 at 09:32
  • 2
    `The doco is all over the place for LUA` that's kind of unfair to the Lua documentation. you just don't read carefully enough. `I couldn't find any mention of that function on the LUA doco about patterns` the section on string [patterns](https://www.lua.org/manual/5.4/manual.html#6.4.1) mentions gmatch and all other functions you can use patterns with in the first sentence and it also says something about multiple matches at the end – Piglet Feb 17 '22 at 09:48
  • @piglet no you didn't read my comment properly. I said the doco is all over the place, and that statement is correct. I never said it wasn't there, and as your link shows it IS there, but it's not easy to find. I've never seen that page...ever. Here is the doco I read, and the one that comes up when you google "LUA patterns" or "LUA regex". So there are two copies of this particular page, so yeah...it is all over the place actually https://www.lua.org/pil/20.1.html – Mucker Feb 21 '22 at 04:36

1 Answers1

5

You can use string.gmatch(s, pattern) / s:gmatch(pattern):

This returns a pattern finding iterator. The iterator will search through the string passed looking for instances of the pattern you passed.

See the online Lua demo:

local a = "Hello\nmy name is\nSomeone\n"
for i in string.gmatch(a, ".*\n") do
  print(i)
end

Note that .*\n regex is equivalent to .*\n Lua pattern. - in Lua patterns is the equivalent of *? non-greedy ("lazy") quantifier.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563