Using regular expressions

Lua:
Using regular expressions

How to:

Lua does not support regular expressions natively in the same way as languages like Perl or Python. Instead, it offers pattern matching capabilities that cover many common use cases of regular expressions. However, for full-fledged regular expression support, one can use a third-party library such as lrexlib.

Basic Pattern Matching in Lua:

Lua provides a powerful pattern matching system that you can use for simple substitutions and searches:

-- Simple search
local str = "Hello, World!"
if string.find(str, "World") then
  print("Match found!")
end
-- Output: Match found!

-- Simple substitution
local s = string.gsub("Lua is great!", "great", "awesome")
print(s)
-- Output: Lua is awesome!

Capturing Substrings:

You can capture parts of the string that match patterns:

local date = "Today is 17/05/2023."
local d, m, y = string.match(date, "(%d+)/(%d+)/(%d+)")
print("Day:", d, "Month:", m, "Year:", y)
-- Output: Day: 17 Month: 05 Year: 2023

Using lrexlib for Regular Expressions:

To use actual regular expressions, you can install and use lrexlib. Assuming you have it installed (luarocks install lrexlib-pcre), you can do more complex pattern matching:

local rex = require 'rex_pcre'

local text = "The rain in Spain stays mainly in the plain."
local regex = "\\bS\\w+"
local count, err = rex.gsub(text, regex, function(w)
  return w:upper()
end)
if err then
  print("Error:", err)
else
  print("Modified text:", text)
  print("Substitutions made:", count)
end
-- Example output: Modified text: The RAIN in SPAIN stays MAINLY in the plain.
-- Substitutions made: 3

The above examples illustrate basic usage within Lua’s own pattern matching system and how to harness the power of regular expressions via lrexlib. Whether you’re performing simple string manipulations or require the full versatility of regular expressions, Lua, coupled with powerful libraries, can accommodate your needs.