Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A regex I don't understand

I'm starring at these few (slightly modified) lines from luadoc that are obviously building a filename with a full path. But I simply don't get it what happens in line 5. The parameter filename could be something like "myfile.lua".

function out_file (filename)
  local h = filename
  h = string.gsub(h, "lua$", "tex")
  h = string.gsub(h, "luadoc$", "tex")
  h = options.output_dir .. string.gsub (h, "^.-([%w_]+%.tex)$", "%1")
  return h
end

What does happen in line 5?

like image 925
h0b0 Avatar asked Apr 15 '11 17:04

h0b0


1 Answers

h = options.output_dir .. string.gsub (h, "^.-([%w_]+%.tex)$", "%1")

The pattern matches any string that begins with zero or more non-alphanumeric characters (i.e. whitespace, etc.) followed by one or more alphanumeric characters and underscores (probably a filename), a period and the string "tex" which then ends. It captures the filename + ".tex" for later use. Basically it's taking a filename with possible junk characters (whitespace) at the beginning and replacing it with the clean version before tacking the output directory to the front of it.

Now what's probably causing you confusion there is that . matches any character. But when modified by a terminating - that means "the shortest string of zero or more characters before the next match" -- i.e. a non-greedy search. It will match on any characters it finds from the beginning of the string until it finds something that matches the compound [%w_] – alphanumeric or underscore.

like image 149
JUST MY correct OPINION Avatar answered Oct 25 '22 20:10

JUST MY correct OPINION