Start finding patterns from the end

  1. What do you want to achieve?
    I need to start finding patterns from the end using string.find in the most optimized way possible.

  2. What is the issue?
    I can’t think of a way of doing so.

  3. What solutions have you tried so far?
    I’ve tried looping over all the characters from the beginning however this simply wasn’t efficient enough because the strings I use are very large. Maybe there’s a way to loop from the end?

This is pretty much what I’m trying to achieve. Note that the character I’m looking for isn’t always second to last.

local example = "qasfjmnbz"
local pattern = "b"
print(string.find(example, pattern))

In the output I want it to return 8 8.
Thanks for the response!

hello hello?

Here is the code, if you want to understand more on what it does, please visit and read the link that @pumpkyrofl replied.

local example = "qasfjmnbz"
local pattern = "b"

local regexCrafted = string.format("%s[^/]*$", pattern)
local foundAt = string.find(example, regexCrafted)
print(foundAt)

Basically, you will start searching from the end of the string with the specific pattern. If found, return the index where it is found, otherwise, return nil.

I’ve tried your solution out, sorry it took me so long. For some reason, it returns the whole string. Here’s my code.

local regex = string.format("%s[^/]*$", "%]") --also tried without the %
print(string.find("]12]42kamdsam]jdajhdo]asdjka", regex))

It prints 1, 28.