Compare and contrast Lua's string manipulation functions: `gsub()` and `match()`. Provide examples to showcase their usage and differences.
Lua provides several string manipulation functions, including `gsub()` and `match()`, which have distinct purposes and usage patterns. Here's a comparison and contrast of these two functions:
1. `gsub()` Function:
The `gsub()` function in Lua is used for pattern-based string substitution or global substitution. It allows you to find and replace occurrences of a pattern within a string with a specified replacement.
The general syntax of `gsub()` is `string.gsub(mainString, pattern, replacement, [count])`. Here, `mainString` represents the original string, `pattern` specifies the pattern to be matched, `replacement` is the string to replace the matched pattern, and `count` (optional) is the maximum number of substitutions to perform.
Example:
```
lua`local str = "Hello, Lua!"
-- Replace all occurrences of 'a' with 'x'
local newStr = string.gsub(str, 'a', 'x')
print(newStr) -- Output: Hello, Lxu!`
```
In this example, `gsub()` replaces all occurrences of the letter 'a' with 'x' in the string `str`.
2. `match()` Function:
The `match()` function is used to extract substrings that match a specified pattern within a string. It returns the matched substring(s) or captures defined within parentheses.
The general syntax of `match()` is `string.match(mainString, pattern)`. Here, `mainString` represents the original string, and `pattern` is the pattern to be matched.
Example:
```
lua`local str = "Hello, Lua!"
-- Extract the word 'Lua' from the string
local matchedStr = string.match(str, 'Lua')
print(matchedStr) -- Output: Lua`
```
In this example, `match()` extracts the substring 'Lua' from the string `str`.
Comparing `gsub()` and `match()`:
* Purpose: `gsub()` is used for pattern-based substitution, allowing you to replace all occurrences of a pattern within a string. `match()`, on the other hand, is used for pattern matching and extracting substrings from a string.
* Pattern Matching: Both functions use Lua's pattern matching syntax, which supports regular expressions and pattern modifiers. However, `match()` returns the matched substring(s), while `gsub()` replaces the matched patterns.
* Multiple Matches: `gsub()` performs global substitution by default, replacing all occurrences of the pattern. In contrast, `match()` only returns the first match unless the pattern contains captures.
* Replacement: `gsub()` requires a replacement string to specify what to replace the matched pattern with. In contrast, `match()` does not involve replacement, as it focuses on extracting and returning the matched substring(s).
It's important to note that both `gsub()` and `match()` rely on Lua's pattern matching syntax, which provides powerful and flexible string manipulation capabilities. By understanding the differences and appropriate use cases for these functions, you can effectively manipulate strings in Lua according to your specific requirements.