String
string.find
Shared
Searches for a pattern in a string and returns its position
Syntax
local start_at, end_at, ... = string.find(input, pattern, start_at = 1, plain_text = false)Parameters
| Type | Name | Description |
|---|---|---|
string | input | String to be searched in |
string | pattern | Pattern to search for (supports Lua patterns unless plain_text is true) |
int | start_at | Position to start searching from - negative counts from end |
bool | plain_text | When true - pattern is treated as plain text, not a pattern |
Returns
| Type | Name | Description |
|---|---|---|
int | start_at | Starting index of the match, or nil if not found |
int | end_at | Ending index of the match, or nil if not found |
... | ... | Any captured substrings from the pattern |
Examples
--Simple text search
local s, e = string.find("hello world", "world")
engine.print(s, e) --7 11--Find first occurrence of a character
local pos = string.find("hello", "l")
engine.print(pos) --3--Search with starting position
local s, e = string.find("hello hello", "hello", 7)
engine.print(s, e) --7 11--Pattern matching with digits
local s, e = string.find("Price: $25.99", "%d+")
engine.print(s, e) --9 10--Pattern with captures
local s, e, num = string.find("hello 123 world", "(%d+)")
engine.print(s, e, num) --7 9 123--Plain text search (disable pattern matching)
local s, e = string.find("Cost: $5.00", "$", 1, true)
engine.print(s, e) --7 7--Multiple captures
local s, e, word1, word2 = string.find("hello world", "(%a+) (%a+)")
engine.print(word1, word2) --hello world--Search from end using negative index
local s, e = string.find("hello", "l", -3)
engine.print(s, e) --3 3