How to reference captured groups in the replacement? #2
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
I try t use a captured group in the repalcement. For example
But this does not the work. the only way I made it works is the cumbdersome function approach:
How can I reference the captures group directly in the replacement argument?
How to add caltured groups in teh repalve,ent?to How to reference captured groups in the replacement?Yes, you can only do it with functions. I haven't found it to be cumbersome and I rather like not having another stringy syntax.
I guess it would be nice to have a TH thingy that would compile e.g.
[rep|\1NEW-NUMMARKER|]to(\(g1:_) -> g1 ++ "NEW-NUMMARKER"). PRs welcome :)Thanks for the clarification!
Using a function is not too bad, but I always get these "Pattern match(es) are non-exhaustive In a lambda abstraction" warning on that function, which is annoying...
I am just starting out with Haskell, so I don't think I can write appropriate code (yet), but I certainly can update the documentation to make it clearer that functions are the only way to get captured groups into the replacement.
FYI, you could also write your example like
This still has the underlying problem that you were getting the warning about—that your replacement function is expecting a list with exactly one element, and that it will crash if it gets a different number of elements—but it’s more explicit about this failure case (because of the explicit use of
error) and it should also suppress that warning.