9

I apply a function, but looks so bad.

function find_without_pattern(s1,s2)
    for i =1,#s1-#s2+1 do
        local t = string.sub(s1,i,#s2+i-1)
        if t == s2 then
            return i,i+#s2-1
        end
    end
end
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
mos
  • 235
  • 3
  • 7

1 Answers1

12

The string.find method provides an optional 4th parameter to enforce a plaintext search by itself.

For example:

string.find("he#.*o", "e#.*o", 1, true)

will give you the correct results.

Quoting the Lua manual pages:

string.find (s, pattern [, init [, plain]])

A value of true as a fourth, optional argument plain turns off the pattern matching facilities, so the function does a plain "find substring" operation, with no characters in pattern being considered magic. Note that if plain is given, then init must be given as well.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • Nice, turns out I did the unnecessary work. I also edited your answer since I've deleted mine. – Yu Hao Sep 24 '13 at 07:06