This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Sewbacca's profile picture

String, optional captures

Started by Sewbacca, 30 May 2016 - 06:38 PM
Sewbacca #1
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
Tiin57 #2
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.
Sewbacca #3
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
MKlegoman357 #4
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.
Sewbacca #5
Posted 30 May 2016 - 09:20 PM
I got an error!
Anavrins #6
Posted 30 May 2016 - 09:50 PM
I got an error!
Are you using an emulator?
Sewbacca #7
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
MKlegoman357 #8
Posted 31 May 2016 - 01:02 PM
Like I mentioned before, what's the rest of the code that is erroring?
Sewbacca #9
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