Jump to content




Regex description


  • You cannot reply to this topic
6 replies to this topic

#1 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 11 June 2013 - 06: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?

#2 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 11 June 2013 - 06:42 AM

In that case, nothing. That's not the proper way of calling string.gmatch.
Learn more about Lua's RegEx here.

#3 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 11 June 2013 - 06: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


#4 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 11 June 2013 - 06: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"


#5 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 11 June 2013 - 06:49 AM

For the record, lua doesn't use Regular Expressions. They're technically called patterns.

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 11 June 2013 - 08:49 AM

View PostOrwell, on 11 June 2013 - 06:44 AM, said:

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.

#7 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 11 June 2013 - 09:36 AM

View Posttheoriginalbit, on 11 June 2013 - 08:49 AM, said:

View PostOrwell, on 11 June 2013 - 06:44 AM, said:

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.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users