string.match()
The string.match() function in Lua searches for a specified pattern in a string and returns the first occurrence of the match. If no match is found, it returns nil. This function is particularly useful for extracting substrings or validating input formats.
Syntax
string.match(s, pattern, init)
Parameters
- Name
s- Type
- string
- Description
The string to be searched for the specified pattern.
- Name
pattern- Type
- string
- Description
The pattern to search for in the string. Lua's pattern matching syntax is used.
- Name
init- Type
- number
- Description
The starting position in the string from which to begin searching. Defaults to
1if not provided.
Return Value
Returns the first match found as a string. If no match is found, it returns nil.
Pattern Table
Here are some commonly used patterns in Lua's pattern matching:
| Pattern | Description |
|---|---|
. | Matches any character (except a newline). |
%a | Matches any letter (alphabetical character). |
%c | Matches any control character. |
%d | Matches any digit (0-9). |
%l | Matches any lowercase letter (a-z). |
%u | Matches any uppercase letter (A-Z). |
%s | Matches any space character (including spaces, tabs, etc.). |
%p | Matches any punctuation character. |
%w | Matches any alphanumeric character (letters and digits). |
%x | Matches any hexadecimal digit (0-9 and a-f). |
* | Matches 0 or more occurrences of the preceding element. |
+ | Matches 1 or more occurrences of the preceding element. |
- | Matches 0 or more occurrences, but as few as possible (non-greedy). |
? | Matches 0 or 1 occurrence of the preceding element. |
^ | Matches the start of a string. |
$ | Matches the end of a string. |
[...] | Matches any single character in the brackets. |
%< and %> | Matches the < and > characters. |
%- | Matches the - character. |
%% | Matches the % character (escape sequence). |
Examples
Example 1: Basic Pattern Matching
Search for a simple substring within a string.
Example 1
local text = "Hello, Lua!"
local match = string.match(text, "Lua")
print(match) -- Output: Lua
Example 2: Using Patterns
Search for patterns using Lua's pattern syntax.
Example 2
local text = "The year is 2025."
local match = string.match(text, "%d%d%d%d") -- Matches a four-digit number
print(match) -- Output: 2025
Example 3: Starting from a Specific Position
Start searching from a specific position in the string.
Example 3
local text = "Look at the stars, Look how they shine."
local match = string.match(text, "Look", 10) -- Start searching after the 9th character
print(match) -- Output: Look
Example 4: No Match Found
Return nil if no match is found.
Example 4
local text = "Hello, Lua!"
local match = string.match(text, "Python")
print(match) -- Output: nil
Use Cases
- Input Validation: Check if a string meets specific format requirements.
- Data Extraction: Extract specific information from strings, such as dates or identifiers.
- Pattern Recognition: Identify the presence of specific patterns in user input or text files.
See also
string.gmatch(): Iterates over all occurrences of a pattern in a string, allowing for more comprehensive pattern searches.string.gsub(): Replaces occurrences of a pattern in a string, which can be useful after identifying matches withstring.match().