I would like to suggest the addition of a new pattern item which will behave just the like the %bxy one but will return the match without the xy part.
(This should be a simple, low overhead addition.)
For example, for
s = ‘1(2)3’
%b() now returns ‘(2)’, and the new proposed pattern item should return just ‘2’, i.e., the () removed, just as if :sub(2,-2) was applied to the result of the %bxy match.
I use %bxy a lot, it’s very very useful in many situations. However, I find that practically all of the time (OK, maybe 99%) I’m only interested in what’s inside the delimiters.
So, for now, I always end up with a :sub(2,-2) at end.
Why not use :sub(2,-2) then?
The problem [apart for flooding my code with a whole bunch of :sub(2,-2)] is I also want to use this in a gsub (for example), and then it’s not as simple.
Yes, I know I can use a function() for gsub.
One example: Strip one level of matching parens with a gsub so that
‘1(2(3)4)5’ becomes ‘12(3)45’
This is a simple example and it can also be tackled with something like
s = s:gsub('%b()',function(s) return s:sub(2,-2) end)
but the pattern can be much more involved that cannot easily be emulated differently.
It’s also possible that in that same string I want to keep one match but not the other.
Example: ‘1(2)3(4)5’ to become ‘1(2)345’
The previous :gsub would no longer work, and a more involved solution is needed. And it gets worse as the pattern gets more complex.
Does anyone else see any merit (or problem, other than the usual minimalism objections) in this proposal?