lua-users home
lua-l archive

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

It was thus said that the Great Russell Haley once stated:
> Hi,


  This is Sean, with the Obligatory LPEG solution 8-)

> I'm trying to parse strings which are C function names from unit tests. The
> strings are like so:
> "test_notInit_notRealBoy_getUsed"
> That's simple enough: string.gmatch(mystr, "(%w+)")
> but sometimes the function names have a double underscore at the last word:
> "test_notInit_notRealBoy__getUsed"
> And I need the last underscore. The last '_' separated token represents a
> function name and they *sometimes* start with an underscore (I have no
> control over the original function names). In this case there is `getUsed`
> and a `_getUsed` function. So in the latter example, I'd like to get this
> back:
> test
> notInit
> notRealBoy
> _getUsed
> Is it possible to do that in one pass?

  Yes.  Here's a solution using LPEG:

local lpeg = require "lpeg"

local segment = lpeg.P"_"^-1 * lpeg.R("az","AZ","09")^1
local fname   = lpeg.Ct((lpeg.C(segment) * lpeg.P"_"^-1)^0)

local test =

for _,id in ipairs(test) do
  local seg = fname:match(id)
  for _,s in ipairs(seg) do

  fname:match() will return an array of each segment of the given function

  -spc (Just FYI ... )
lua-l mailing list --
To unsubscribe send an email to