본문 바로가기
개발/LUA

[LUA] 문자열 다루기 간단 예제 #2

by esstory 2020. 5. 24.

루아에서 문자열 찾는 함수는 string.find 입니다. 

얼마전에 문자열에서 '.' 을 찾을 일이 있어서 이 함수를 이용했는데 return 값이 좀 특이했습니다.

local nStart, nEnd = string.find('123.23', '.')
print(nStart, nEnd)
-- result: 1 1

Lua 문서를 찾아 보니 Lua의 find 는 기본이 패턴이네요(패턴의 일부 기능만 지원)

그래서 '.' 을 찾을 때는 "%." 과 같이 lua 패턴 escape문자 %를 같이 처리하거나, Plain Text 검색임을 명시해야 합니다.

local nStart, nEnd = string.find('123.23', '%.')
print(nStart, nEnd)
-- result: 4 4

local nStart, nEnd = string.find('123.23', '.', 1, true)
print(nStart, nEnd)
-- result: 4 4

기본 find 에서 패턴 검색 기능을 제공하지만, 정규표현식의 아주 일부만 처리가 가능합니다. 

루아의 패턴 검색 관련 내용은 아래 글을 참고해 주세요 

https://www.lua.org/pil/20.2.html

 

Programming in Lua : 20.2

This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences. The fourth edition targets Lua 5.3 and is available at Amazon and other bookstores. By buying the book, you also help to support the Lu

www.lua.org

 

자주 쓰이는 패턴들은 다음과 같습니다.

%d

숫자

%s

공백

.

모든 문자

%w

워드(알파벳과 숫자)

%S

공백이 아닌 문자

%D

숫자가 아닌 것

^

문자열의 시작

[^..]

~으로 시작하지 않는

%

이스케이프용 문자

 

아래는 패턴을 이용한 간단한 예제 코드인데, 010-1234-5678 같은 숫자를 찾는 코드입니다.

%d 는 숫자에 대응하고 %- 는 '-' 문자에 대응합니다. 

루아 패턴에는 반복 횟수 지정이 없어서 아래처럼 %d를 반복처리해야 합니다.

local sTestData = "1010<101>04 32 1 904 32 10 1324 32104 32 1[4032]4 010-1234-5678 32-104 321-4032 4032140132 412-0430-1214 40[32]141234 4320143201"
local nStart, nEnd


-- 전화번호 찾기 
nStart, nEnd = string.find(sTestData, "%d%d%d%-%d%d%d%d%-%d%d%d%d")
print(nStart, nEnd)
-- result: 51 63

 

string.gmatch 를 이용하면 패턴에 매치한 문자열을 반복해서 가져올 수 있습니다.

-- gmatch를 이용하면 패턴에 매칭된 문자를 반복적으로 찾을 수 있음. 
for match in string.gmatch(sTestData, "%d%d%d%-%d%d%d%d%-%d%d%d%d") do
    print(match)
end
-- result
-- 010-1234-5678
-- 412-0430-1214

 

대괄호([]) 안에 숫자가 있는 문자를 찾으려면 아래와 같이 호출합니다.

-- gmatch로 [] 안에 숫자 찾기 
for match in string.gmatch(sTestData, "%[%d+%]") do
    print(match)
end
-- result
-- [4032]
-- [32]

 

공백이 아닌 문자를 찾는 경우(공백으로 분리)는 아래처럼 [^%s]+ 나 %S+ 로 찾을 수 있습니다.

-- gmatch 로 공백단위로 분리, %S 는 공백이 아닌 문자 [^%s]+ 또는 %S+
-- for match in string.gmatch(sTestData, "%S+") do
for match in string.gmatch(sTestData, "[^%s]+") do
        print(match)
end
-- result
-- 1010<101>04
-- 32
-- 1
-- 904
-- 32
-- 10
-- 1324
-- 32104
-- 32
-- 1[4032]4
-- 010-1234-5678
-- 32-104
-- 321-4032
-- 4032140132
-- 412-0430-1214
-- 40[32]141234
-- 4320143201

 

'개발 > LUA' 카테고리의 다른 글

[LUA] TABLE 정렬(SORT)  (0) 2019.10.19
[LUA] 개발 환경 만들기  (1) 2019.09.30
[LUA] FOR 문  (0) 2019.09.29
[LUA] 함수 호출 CALL BY VALUE/CALL BY REFERENCE  (0) 2019.07.28
[LUA] 테이블 다루기 - 배열/맵/클래스  (0) 2019.07.28
[LUA] 문자열 다루기 간단 예제  (0) 2019.07.27

댓글