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

Regex description

Started by H4X0RZ, 11 June 2013 - 05:15 AM
H4X0RZ #1
Posted 11 June 2013 - 07:15 AM
I've found this:

local config = fs.open("file", "r")
local sLine = config.readLine()
while sLine do
for a in string.gmatch("something = (.+)") do something = a end
end
print(something)
what does the regex do?
LBPHacker #2
Posted 11 June 2013 - 07:42 AM
In that case, nothing. That's not the proper way of calling string.gmatch.
Learn more about Lua's RegEx here.
Orwell #3
Posted 11 June 2013 - 07:44 AM
It will find a portion in the file that reads 'something = abcdef' where abcdef is any sequency of characters with a length greater than one. The brackets mean that only the abcdef part will be extracted and that the function will only return that value. So for each instance of 'something = xxsomestringxx', the variable 'a' will equal 'xxsomestringxx'. In each iteration of the loop a will be assigned to the variable something. So eventually something will equal the value from the last match in the file and will be nil if no match has been found.

Edit:
LBP is right, there are some other issues with the code. You don't need to loop through the lines of the file and obviously you need to pass the content from the file to string.gmatch. This is a updated version of yours:

local config = fs.open("file", "r")
local sFile = config.readAll()
local something = sFile:gmatch("something = (%S+)")() -- %S matches every character except for space characters
print(something or "No match has been found!")

Now if you have a file on your computer named 'file' that has this content:

This is a file with some stuff in.
foo = bar
something = foobar
me ~= you
LBPHacker #4
Posted 11 June 2013 - 07:46 AM
By the way, if you want to load a config file that looks like this…
something = "Lua"
animal = "duck"
car = "bmw"
… you can just execute it with an own environment table:
local func = loadfile(filename)
local env = {}
setfenv(func, env)
func()
print(env.something) -- that will print "Lua"
Kingdaro #5
Posted 11 June 2013 - 07:49 AM
For the record, lua doesn't use Regular Expressions. They're technically called patterns.
theoriginalbit #6
Posted 11 June 2013 - 09:49 AM
This is a updated version of yours:

local config = fs.open("file", "r")
local sFile = config.readAll()
local something = sFile:gmatch("something = (%S+)")() -- %S matches every character except for space characters
print(something or "No match has been found!")
I would say that you would still want to for loop so that it gets the last copy of the match, not the first, or of course you should be able to use the `$` in the pattern.
Orwell #7
Posted 11 June 2013 - 10:36 AM
This is a updated version of yours:

local config = fs.open("file", "r")
local sFile = config.readAll()
local something = sFile:gmatch("something = (%S+)")() -- %S matches every character except for space characters
print(something or "No match has been found!")
I would say that you would still want to for loop so that it gets the last copy of the match, not the first, or of course you should be able to use the `$` in the pattern.
Indeed. I understood from the OP that he doesn't really have a use for this but rather wants to know what's going on. So I tried breaking it down a little and I left some parts out.