2014-11-18 5 views
3

오전 1시 루아를 사용하여 문자열에 문자열을 분할하려고합니다. for 루프는 1 일 4 경기 이하로 예상하지만 지난 몇 년 동안 sadipscing consetetur 2.분할 문자열은

print(words[1]) 표시

"### 로렘 입숨 당근, Grusia을 얻을 것의 패턴을 사용하지만, nonumy 사실 그들 나오지 유타 labore 등 dolore 마그나 labore 마그나 tempor 베로 erat, 나오지도 DIAM의 \ n "+

print(words[2]) 표시

"### 만, 그리고 고통을 느끼고 단지 그들 중 두 사람은 보조금. ?.

누군가가 나에게이 동작을 설명해 주시겠습니까 "N 베로 \ nkasd 듀오, consetetur Gruß 성령 \에서

i=0 
content = "###Lorem ipsum dolor sit amet, Gruß consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam \n ###voluptua. ###At vero eos et accusam et justo duo dolores et ea rebum. Stet clita \nkasd gubergren, no sea takimata Gruß sanctus est \n###XLorem ipsum dolor sit amet. Lorem ipsum \ndolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor \ninvidunt ut labore et Gruß dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.sdl" 
for word in string.gmatch(content, '###') 
do i = i+1 end 

if(i>1) then 
content = content .. '###' 
else end 

words= {}  
for y in string.gmatch(content,"(###.-)###") 
do 
    table.insert(words, y) 
end 

print(words[3]) 

답변

0

뉴스 루프가 네 경기를 발견하는 일은이 사기보십시오 :

를 그런 당신을 위해 작동하는 경우 필요에 따라
for word in string.gmatch(content, '###([^#]+)') do 
    print(word) 
end 

, 다음 루프에서 word을 저장합니다.

2

을 이것은 단순화 두 번째 루프의 버전,

content = '###aa###bb###cc###dd###' 
words= {}  
for y in string.gmatch(content,"(###.-)###") do 
    print(y) 
    table.insert(words, y) 
end 

출력 : 패턴 (###.-)###

###aa 
###cc 

문제는이 두 번째 ###도 소모됩니다. 당신이 필요한 것은 정규식 내다 (###.+?)(?=###) 같다. 불행하게도 루아 패턴은 내다보기를 지원하지 않습니다.

local left = content 
local start = 1 
while true do 
    start, index, match = string.find(left, "(###.-)###") 
    if not start then break end 
    print(match) 
    left = left:sub(index - 3) --3 is the length of "###" 
end 
: 이것은 하나의 가능한 해결 방법입니다