Home > Net >  What is % in Lua [not for numeric operations]?
What is % in Lua [not for numeric operations]?

Time:08-26

So I was using string.find() to find "(" in my string. But without "%" before "(" (like "%(") it says: "unfinished capture". What exactly this symbol doing?

Works:

local str = "(text)"
print(str:find("%("))

Don`t work:

local str = "(text)"
print(str:find("("))

CodePudding user response:

It's used in patterns, used in some functions related to finding. For example, %s means "find a single whitespace character". %( searches for the character (. The reason you can't directly write ( is that that will create a capture, which is a mechanism to retrieve a part of a match. For other characters, you directly type them, unless there is a similar restriction.

  • Related