Can somebody explain this behavior to me?

I fail to understand why this returns “a” instead of nil.

print(string.match("acx1",".")) -- prints "a"

string.match looks for the first match of pattern in the string s. If a match is found, it is returned; otherwise, it returns nil. A third, optional numerical argument, init, specifies where to start the search; its default value is 1 and can be negative.

This does not answer my question. it should return nil in this case, I must be missing something.

I think it’s returning nil because you are forgetting to provide all three parameters for string.match.

The first parameter is s for string, the second is pattern which is a string, and third is init which is a number defaulted at a value of 1.

This may also help:

It does not return nil it returns “a”, please read my post.

You should read the help on patterns. A period means it will match any single character, in this instance the ‘a’.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.