LuaNET, the lua 5.1 api documentation!

Lua-Api

String

[LUA 5.1] string.find

function string.find ( s, pattern, init, plain )
Looks for the first match of pattern in the string s. If it finds a match, then find returns the indices of s where this occurrence starts and ends; otherwise, it returns nil.

A third, optional numerical argument init specifies where to start the search; its default value is 1 and may be negative.

Code
-- Find "an" in the string.
s = "This is an example"
i, j = string.find(s, "an")
print(i, j) -- 9 10


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.

If the pattern has captures, then in a successful match the captured values are also returned, after the two indices.

Code
-- Find the first two words, seperated
-- by a space using regular expressions.
s = "This is an example"
pos, after, match1, match2 = string.find(s,"(%w*) (%w*)")
print(pos, after, match1, match2) -- 1 7 This is

Comments

© 2007 DracoBlue :: Valid XHTML, CSS, RSS Powered by SMF 1.1.5 | SMF © 2006-2008, Simple Machines LLC | SourceNet © 2007, DracoBlue :: Page created in 0.236 seconds with 29 queries.