Total Pageviews

Monday, April 21, 2014

string.match()

string.match()
វាប្រើសំរាប់ extract substring ដោយធ្វើការ matching a pattern នៅក្នុង String។ បើសិនជា match ត្រូវបានរកឃើញ, ពេលនោះវានឹង captures ចេញពី pattern, បើមិនទេវានឹងត្រឡប់ជា nil។ បើសិនជា pattern មិនត្រូវបាន captures ពេលនោះ whole match ត្រូវបានត្រឡប់
Syntax: string.match (s, pattern [, init])
S:match (pattern [, init])
S(តំរូវការ) វាជា​ String
Pattern(តំរូវការ) វាជា String ដែល pattern ត្រូវការ​ match
Init(ជំរើស) វាជាចំនួនលេខដែលនៅក្នុង ក្នុងការចាប់ផ្តើមស្វែងរក។ តំលៃដើមគឺ ហើយអាចជា negative
សូមមើលកូដះ
print (string.match("I have 2 questions for you.", "%d+ %a+"))
-- OUTPUT: 2 questions

print (string.format("%d, %q", string.match("I have 2 questions for you.", "(%d+) (%a+)")))
-- OUTPUT: 2, "questions"


No comments:

Post a Comment