lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi there,
I have a function that takes a string, checks for sequences of one or more characters, and inserts something in front of it. For example, if the string is ":::...MMM", it will be changed to "{g}:::{b}...{x}MMM". I am using gsub() to insert the text, using patterns like "(M+)", but I couldn't figure out a pattern that would capture any sequence of characters. So, my function basically looks like:

s = gsub(s, "(:+)", function(l) return "{g}"..l end)
s = gsub(s, "(%.+)", function(l) return "{b}"..l end)
s = gsub(s, "(%%+)", function(l) return "{B}"..l end)

...and so on, for about 10 more patterns. This is quite bad, since I would rather have another function that checks what the character is, then returns the appropriate string, like this:

function check(s)
  ss = strsub(s, 1, 1)
  if ss == ":" then return "{g}"..s
  elseif ss == "." then return "{b}"..s
  elseif ...
  ...
end

function blah(s)
  return gsub(s, "(some pattern)", check)
end

Is there a pattern that can be used to capture sequences of one or more different characters?

Regards,
Jason

_____________________________________________________________