New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: regexp: add Replace and ReplaceString #40198
Comments
This comment has been minimized.
This comment has been minimized.
You're talking about adding six more methods to regexp.Regexp, which is already drowning in methods. Is there any strong evidence that this sort of thing is needed? |
This comment has been minimized.
This comment has been minimized.
@rsc Yes it is easy to implement, like most of the "basic" functions. This can be seen as argument in both directions to add or to not add to the standard library. My main argument is that this is a basic functionality present in most of the languages precisely because it is used very often. But I don't have "strong evidence that this is needed". Maybe somebody else can give such evidence ... |
Adding to the proposal minutes for visibility. |
Perhaps a solution to consider could be adding one example to the doc showing the basic approach? That said, there are already many examples in the package, so discoverability and overall noise level might be a concern. |
For code like this, this issue itself can serve as a way to address the problem. I'm not sure it has to be in the package docs. |
Based on the discussion above, this seems like a likely decline. |
@rsc ok. Thanks for considering my request anyway. |
I use the limit param in PHP all the time: https://www.php.net/manual/en/function.preg-replace.php I am annoyed that I cannot use it in Go. |
PHP contains much that Go does not. (Also, if you are annoyed about needing this limit in Go frequently, I would suggest writing a function along the lines of the one at the top of this issue, and then you won't need to be annoyed anymore.) |
I am just replying to "evidence of widespread use in other languages". Here is another example: |
I can only report that we offer a template function in Hugo that supports a regexp replace with a maximum. We used the closure mentioned at the top of this issue. I understand the argument against added a half-dozen functions to the package. So, I offer a compromise: change the |
In effect there is no Go 2, there is just the possibility of regexp/v2. But that doesn't seem all that likely, as the regexp package is working fine. Still, examples of cases where people would use limits are helpful. It might help to point to some places where people do use a limit, and why. That is, it does help to show other languages and frameworks that provide a limit. But it helps even more to show where people use that limit in their code. Thanks. |
Use case example: in Hugo, we want to find to replace the first occurence of each glossary term on a given web page with a link to the definition in the Glossary. We do not want to link 100 occurrences of a word or acronym in an article. |
My initial use case was similar to @fplanque's case : I wanted to replace only the first occurance in a program charged to convert from one file format to another. |
Maybe at some point we should revisit the regexp API, but I don't think this incremental addition pulls its weight especially given the Func trick. In retrospect the All form should have taken an n, like FindAll etc, but it's too late for that now. No change in consensus, so declined. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?Any
What did you do?
I try to replace not all occurrences using regexp library
What did you expect to see?
To have
Replace
andReplaceString
methods that take an extra integer parameter that indicat the maximal number of replacements, like in the python'sre.subs
.Here is my proposal for
ReplaceString
:The text was updated successfully, but these errors were encountered: