[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Gmatch Pattern Help
- From: Sean Conner <sean@...>
- Date: Fri, 24 Apr 2020 17:17:11 -0400
It was thus said that the Great Russell Haley once stated:
> Hi,
Hello.
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 =
{
"test_notInit_notRealBoy_getUsed",
"test_notInit_notRealBoy__getUsed",
"_test_notInit_notRealBoy__getUsed",
"_99_red_balloons__english_version",
}
for _,id in ipairs(test) do
print(id)
local seg = fname:match(id)
for _,s in ipairs(seg) do
print("",s)
end
end
fname:match() will return an array of each segment of the given function
name.
-spc (Just FYI ... )
_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org