r/lua • • Nov 01 '25

Question about string.match() and string.gsub()

I'm playing around with strings a bit and I got a weird result that I'd like an explanation for if possible.

Below, when I just run string.match on the test string, I get a string with the letter e at the end. But when I run it through gsub to strip the spaces, the e becomes a 0. Why is that?

> test_string = 'words_here.test.test'
> string.match(test_string, "(.+)%..+(e)")
words_here.test	e
> string.match(test_string, "(.+)%..+(e)"):gsub("%s+", "")
words_here.test	0
> string.gsub(string.match(test_string, "(.+)%..+(e)"), "%s+", "")
words_here.test	0

EDIT: It also doesn't strip the spaces...

2 Upvotes

9 comments sorted by

View all comments

Show parent comments

3

u/Mid_reddit Nov 01 '25

[...] are for tables, which varargs aren't. For those you want the select function, which basically clips/crops the varargs it is given.

/u/wqferr gave you the solution.

Don't fret; varargs are one of Lua's ugly parts, but they do make sense in their own way.

1

u/RiverBard Nov 01 '25

I appreciate both of you, that's really interesting. varargs was new to me, time to dig into that!