-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
use Char
replacement function for Char
pattern
#25815
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -390,6 +390,8 @@ end | |
_replace(io, repl, str, r, pattern) = print(io, repl) | ||
_replace(io, repl::Function, str, r, pattern) = | ||
print(io, repl(SubString(str, first(r), last(r)))) | ||
_replace(io, repl::Function, str, r, pattern::Function) = | ||
print(io, repl(getindex(str, first(r)))) | ||
|
||
replace(str::String, pat_repl::Pair{Char}; count::Integer=typemax(Int)) = | ||
replace(str, equalto(first(pat_repl)) => last(pat_repl); count=count) | ||
|
@@ -438,8 +440,11 @@ end | |
Search for the given pattern `pat` in `s`, and replace each occurrence with `r`. | ||
If `count` is provided, replace at most `count` occurrences. | ||
`pat` may be a single character, a vector or a set of characters, a string, | ||
or a regular expression. If `r` | ||
or a regular expression. | ||
If `pat` is a `Regex` or `AbstractString` and `r` | ||
is a function, each occurrence is replaced with `r(s)` where `s` is the matched substring. | ||
If `pat` is a `Char`, a collection of `Char` or a predicate function and `r` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of repeating the full sentence, maybe just change |
||
is a function, each occurrence is replaced with `r(s)` where `s` is the matched character. | ||
If `pat` is a regular expression and `r` is a `SubstitutionString`, then capture group | ||
references in `r` are replaced with the corresponding matched text. | ||
To remove instances of `pat` from `string`, set `r` to the empty `String` (`""`). | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -261,6 +261,15 @@ end | |
|
||
# Issue 25741 | ||
@test replace("abc", ['a', 'd'] => 'A') == "Abc" | ||
|
||
# for Char pattern call Char replacement function | ||
@test replace("a", "a" => sizeof) == "1" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than |
||
@test replace("a", r"a" => sizeof) == "1" | ||
@test replace("a", 'a' => sizeof) == "4" | ||
@test replace("a", occursin("a") => sizeof) == "4" | ||
@test replace("a", ['a'] => sizeof) == "4" | ||
|
||
|
||
end | ||
|
||
@testset "chomp/chop" begin | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
str[first(r)]
?