On 24/04/2020 23:34, Russell Haley wrote:
> Hi,
>
> I'm trying to parse strings which are C function names from unit
> tests. The strings are like so:
> "test_notInit_notRealBoy_getUsed"
>
> 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?
>
> Cheers,
> Russ
>
string.gmatch(mystr, "(_?%w+)_?")
This is better than my solution because it avoids the expensive string copying.