Add notes on Space Lua Pattern Matching (#1366)

This commit is contained in:
wbhouston
2025-05-02 07:36:04 +02:00
committed by GitHub
parent 73f6b09d02
commit c0f68f0906
3 changed files with 63 additions and 28 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ import printf from "./printf.ts";
const ROSETTA_STONE = {
"([^a-zA-Z0-9%(])-": "$1*?",
"([^%])-([^a-zA-Z0-9?])": "$1*?$2",
"(.)-$": "$1*?",
"([^%])-$": "$1*?",
"%a": "[a-zA-Z]",
"%A": "[^a-zA-Z]",
"%c": "[\x00-\x1f]",
+6 -3
View File
@@ -155,10 +155,13 @@ assertEqual(string.match("abc123", "%a+"), "abc")
assertEqual(string.match(" abc", "%s+"), " ")
-- Test multiple captures
local day, month, year = string.match("2024-03-14", "(%d+)%-(%d+)%-(%d+)")
assertEqual(day, "2024")
local year, month, day = string.match("2024-03-14", "(%d+)%-(%d+)%-(%d+)")
assertEqual(year, "2024")
assertEqual(month, "03")
assertEqual(year, "14")
assertEqual(day, "14")
-- Test escaped hyphen at the end of a pattern
assertEqual(string.match("4-", "%d%-"), "4-")
-- Test optional captures
local word = string.match("The quick brown fox", "%s*(%w+)%s*")