You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
Hi, I want to know what the modifier %n does for and be able to use it correctly with string.match.
What is the issue? Include screenshots / videos if possible!
I cant seem to grasp how to use it from the dev-wiki.
The wiki states its definition as:
For n between 1 and 9 , matches a substring equal to the n -th captured string.
Then doesn’t give any practical examples.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve attempted to search around the forums to no avail. The only thing remotely even resembling it was talking about newline, or \n.
In Lua % is used as the escape character, as its namesake suggests it “escapes” the intended purpose of a character, in this case the letter “n”, and gives it a special meaning, the escape sequence %n is used in string patterns to match a newline in the subject string (string being searched).
I’m curious about this too, could someone tag me if they give an answer?
I’ve tried a few code samples from a few websites upon doing some research on substrings but I genuinely have no idea how or why they work.
local s = 'aaaaa'
print(s:find('()aa()'))
--> 1 2 1 3???
I’ve also tried to do stuff like
local s = 'aaaaa'
print(s:find('%n()aa()'))
--> nil
I haven’t seen a single code sample using %n so it can’t be used that much, can it?
I think %n represents an integer value but I’ve never payed any regard to it, nor can I figure out why it even exists
local s = '%n' print(s:format(1)) --> errors
local s = '1'
print(s:find('%n')) --> nil
local s = '1'
print(s:find('%n()1()')) --> nil
local s = 'aaa'
print(s:find('%n()a()')) --> nil
I also tried thinking n was a placeholder for a number, however doing anything like '%1()a()' also errors