From aad90d902ec9791dbd60a6c8937aa7fbaca8cb82 Mon Sep 17 00:00:00 2001 From: Micah Date: Thu, 19 Sep 2024 11:54:45 -0700 Subject: [PATCH 1/4] Add docs for empty captures in string patterns --- content/en-us/luau/strings.md | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/content/en-us/luau/strings.md b/content/en-us/luau/strings.md index 546a242d6..3cf9f28f7 100644 --- a/content/en-us/luau/strings.md +++ b/content/en-us/luau/strings.md @@ -549,3 +549,44 @@ the following: + +In addition to all of the above, there is a special case with an **empty capture** (`()`). If a capture is empty, then the position in the string will be captured: + +```lua +local match1 = "Where does the capture happen? Who knows!" +local match2 = "This string is longer than the first one. Where does the capture happen? Who knows!" + +local pattern = "()Where does the capture happen%? Who knows!()" + +local start1, finish1 = string.match(match1, pattern) +print(start1, finish1) +--> 1 42 + +local start2, finish2 = string.match(match2, pattern) +print(start2, finish2) +--> 43 84 +``` + +These special captures may be nested like normal ones: + +```lua +local places = "The Cloud Kingdom is heavenly, The Forest Kingdom is peaceful" +local pattern = "The (%a+()) Kingdom is %a+" + +for kingdom, position in string.gmatch(places, pattern) do + print(kingdom, position) +end +--> Cloud 10 +--> Forest 42 +``` + +The returned values are unusual in that they are **numbers** rather thing strings: + +```lua +local match = "This is an example" +local pattern = "This is an ()example" + +local position = string.match(match, pattern) +print(typeof(position)) +--> number +``` From 332cc1d2571ed41e70921db2210b7e177c4047b8 Mon Sep 17 00:00:00 2001 From: Micah Date: Sat, 21 Sep 2024 13:26:08 -0700 Subject: [PATCH 2/4] Correct typo (thing -> than) Co-authored-by: sircfenner --- content/en-us/luau/strings.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/en-us/luau/strings.md b/content/en-us/luau/strings.md index 3cf9f28f7..45984529f 100644 --- a/content/en-us/luau/strings.md +++ b/content/en-us/luau/strings.md @@ -580,7 +580,7 @@ end --> Forest 42 ``` -The returned values are unusual in that they are **numbers** rather thing strings: +The returned values are unusual in that they are **numbers** rather than strings: ```lua local match = "This is an example" From 804cc1ffc0b132b37719e3b37fb9146f0d78a6e7 Mon Sep 17 00:00:00 2001 From: IgnisRBX <43388550+IgnisRBX@users.noreply.github.com> Date: Tue, 24 Sep 2024 05:38:42 -1000 Subject: [PATCH 3/4] Apply suggestions from code review --- content/en-us/luau/strings.md | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/content/en-us/luau/strings.md b/content/en-us/luau/strings.md index 45984529f..8c1edbdbb 100644 --- a/content/en-us/luau/strings.md +++ b/content/en-us/luau/strings.md @@ -554,23 +554,21 @@ In addition to all of the above, there is a special case with an **empty capture ```lua local match1 = "Where does the capture happen? Who knows!" -local match2 = "This string is longer than the first one. Where does the capture happen? Who knows!" +local match2 = "This string is longer than the first one. Where does the capture happen? Who knows?!" local pattern = "()Where does the capture happen%? Who knows!()" local start1, finish1 = string.match(match1, pattern) -print(start1, finish1) ---> 1 42 +print(start1, finish1) --> 1 42 local start2, finish2 = string.match(match2, pattern) -print(start2, finish2) ---> 43 84 +print(start2, finish2) --> 43 84 ``` These special captures may be nested like normal ones: ```lua -local places = "The Cloud Kingdom is heavenly, The Forest Kingdom is peaceful" +local places = "The Cloud Kingdom is heavenly, The Forest Kingdom is peaceful." local pattern = "The (%a+()) Kingdom is %a+" for kingdom, position in string.gmatch(places, pattern) do @@ -587,6 +585,4 @@ local match = "This is an example" local pattern = "This is an ()example" local position = string.match(match, pattern) -print(typeof(position)) ---> number -``` +print(typeof(position)) --> number From 12f9900a4df9067a719a25a94c818385fda85534 Mon Sep 17 00:00:00 2001 From: IgnisRBX <43388550+IgnisRBX@users.noreply.github.com> Date: Tue, 24 Sep 2024 05:39:23 -1000 Subject: [PATCH 4/4] Update strings.md --- content/en-us/luau/strings.md | 1 + 1 file changed, 1 insertion(+) diff --git a/content/en-us/luau/strings.md b/content/en-us/luau/strings.md index 8c1edbdbb..ec5216377 100644 --- a/content/en-us/luau/strings.md +++ b/content/en-us/luau/strings.md @@ -586,3 +586,4 @@ local pattern = "This is an ()example" local position = string.match(match, pattern) print(typeof(position)) --> number +```