463 posts
Location
Star Wars
Posted 30 May 2016 - 08:38 PM
I have such code:
for str, pattern in str:gmatch('(.-)(%b[])') do
<CODE>
end
It fails, when I call:
lua> foo('a string [/*-] a string [-*/] a string')at the last capture
(
'a string ', '/*-'' a string ', '-*/'' a string', <here occurs the error>), because it doesn't ends with
%b[].
Is there a way, to say that the last capture is optional?
Edited on 30 May 2016 - 06:39 PM
1243 posts
Location
Indiana, United States
Posted 30 May 2016 - 08:42 PM
I have such code:
for str, pattern in str:gmatch('(.-)(%b[])') do
<CODE>
end
It fails, when I call:
lua> foo('a string [/*-] a string [-*/] a string')at the last capture
(
'a string ', '/*-'' a string ', '-*/'' a string', <here occurs the error>), because it doesn't ends with
%b[].
Is there a way, to say that the last capture is optional?
You should be able to use this:
gmatch('(.-)[(%b[])]?')
Since the ? operator matches 0 or 1 of the preceding element, it should be fine. You may not need the [] I added, though.
463 posts
Location
Star Wars
Posted 30 May 2016 - 08:47 PM
THX, I didn't know, how to use the ? on captures!!!!
EDIT:
I have such code:
for str, pattern in str:gmatch('(.-)(%b[])') do
<CODE>
end
It fails, when I call:
lua> foo('a string [/*-] a string [-*/] a string')at the last capture
(
'a string ', '/*-'' a string ', '-*/'' a string', <here occurs the error>), because it doesn't ends with
%b[].
Is there a way, to say that the last capture is optional?
You should be able to use this:
gmatch('(.-)[(%b[])]?')
Since the ? operator matches 0 or 1 of the preceding element, it should be fine. You may not need the [] I added, though.
Both ideas doesn't work, first I get an invaild capture error, then a vm error.
Edited on 30 May 2016 - 06:54 PM
1140 posts
Location
Kaunas, Lithuania
Posted 30 May 2016 - 09:03 PM
We should probably start with: what's the error you're getting? This code:
local function foo (str)
for str, pattern in str:gmatch('(.-)(%b[])') do
print(str, pattern)
end
end
foo('a string [/*-] a string [-*/] a string')
Runs perfectly fine for me: as expected returns only the first two matches. It's probably the <CODE> part that is actually erroring.
463 posts
Location
Star Wars
Posted 30 May 2016 - 09:20 PM
I got an error!
756 posts
Posted 30 May 2016 - 09:50 PM
I got an error!
Are you using an emulator?
463 posts
Location
Star Wars
Posted 30 May 2016 - 10:27 PM
No, I am using:
Minecraft: 1.7.10
OCS: ?
CC: highest version on 1.7.10 (I think 1.74)
custom ROM: Which edits just one line in the startup file.
Edited on 30 May 2016 - 08:28 PM
1140 posts
Location
Kaunas, Lithuania
Posted 31 May 2016 - 01:02 PM
Like I mentioned before, what's the rest of the code that is erroring?
463 posts
Location
Star Wars
Posted 31 May 2016 - 01:03 PM
See the thumbnail (test.txt is your code):
local function foo (str)
for str, pattern in str:gmatch('(.-)(%b[])') do
print(str, pattern)
end
end
foo('a string [/*-] a string [-*/] a string')
Edited on 31 May 2016 - 11:05 AM