Captures और gmatch
gmatch एक string में एक pattern के हर match पर loop करता है।
In this page:
Captures और gmatch
string.gmatch एक iterator return करता है जो बारी-बारी हर match, या captures का हर set, yield करता है। एक pattern में parentheses captures define करती हैं, और quantifiers *, +, -, और ? हैं। Common uses एक string को words में split करना या key=value pairs पढ़ना हैं।
Note:
Pattern [^,]+ characters के किसी भी run से match करता है जो commas नहीं हैं, जो splitting के लिए handy है।
उदाहरण: Captures and gmatch
for word in ("one two three"):gmatch("%a+") do
print(word)
end
for k, v in ("a=1, b=2"):gmatch("(%w+)=(%w+)") do
print(k, v)
end
local fields = {}
for f in ("x,y,,z"):gmatch("[^,]+") do fields[#fields + 1] = f end
print(#fields, table.concat(fields, "|"))
-- Output:
-- one
-- two
-- three
-- a 1
-- b 2
-- 3 x|y|z
Login to try C/C++/Java/PHP code in the editor
आम गलतियां
- gmatch को एक for loop में इस्तेमाल किए बिना call करना
- lazy quantifier - को एक range से confuse करना
- जब आपको captures चाहिए तो parentheses भूल जाना
चैप्टर सारांश
- gmatch matches पर एक iterator देता है
- Parentheses captures बनाते हैं
- * + - ? quantifiers हैं
- [^x]+ x के बिना एक run से match करता है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: