본문 바로가기
Article/CODING AND SCRIPTS

Basic String Patterns (정규식)

by Roblox_개발자 2021. 4. 6.

Simple Matching

local match = string.match("The Cloud Kingdom has 25 power gems", "%d")
print(match)
local match1 = string.match("Welcome to Roblox!", "Roblox")
local match2 = string.match("Welcome to my awesome game!", "Roblox")
print(match1) -- roblox
print(match2) -- nii

위와 같이 subString과 일치하는 문자를 찾는 단순한 방법이 있다. 하지만 이런 방식으로는 원하는 문자를 찾는데 상당한 제약이 있다. 

The Cloud Kingdom has 25 power gems

The Haunted Mines have 10 power gems

The Wizard Village has 50 power gems

위 세 문장에서 잼의 개수만 가져오고 싶을 경우에는 위의 방식을 사용하는 것은 적절하지 않다.

Pattern Matching

Class Represents Example Match
. Any character 32kasGJ1%fTlk?@94
%a An uppercase or lowercase letter aBcDeFgHiJkLmNoPqRsTuVwXyZ
%l A lowercase letter abcdefghijklmnopqrstuvwxyz
%u An uppercase letter ABCDEFGHIJKLMNOPQRSTUVWXYZ
%d Any digit (number) 0123456789
%p Any punctuation character !@#;,.
%w An alphanumeric character (either a letter or a number) aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789
%s A space or whitespace character _, \n, and \r

 

local match = string.match("The Cloud Kingdom has 25 power gems", "%d")
print(match) -- 결과: 2

위와 같이 패턴을 이용해서 잼의 개수를 가져올 수 있다. 하지만 원하는 25의 값이 아닌 2의 값만 가져오는 것을 알 수 있다. 

 

Quantifiers

Quantifier Meaning
+ Match 1 or more of the preceding character class
- Match as few of the preceding character class as possible
* Match 0 or more of the preceding character class
? Match 1 or less of the preceding character class
local match1 = string.match("The Cloud Kingdom has 25 power gems", "%d")
print(match1) -- 결과: 2
local match2 = string.match("The Cloud Kingdom has 25 power gems", "%d+")
print(match2) -- 결과: 25

위와 같이 Quantifier를 이용하면 자신이 원하는 문자를 적절하게 가져올 수 있다. 

 


참고 링크

developer.roblox.com/en-us/articles/basic-string-patterns

'Article > CODING AND SCRIPTS' 카테고리의 다른 글

Understanding CFrames  (0) 2021.04.10
Roblox Client-Server Model  (0) 2021.04.10
BodyPosition  (0) 2021.04.06
BodyMovers  (0) 2021.04.06
Argument와 Parameter  (0) 2021.04.06

댓글