@@ -373,8 +373,8 @@ _replace(io, repl, str, r, pattern) = print(io, repl)
373
373
_replace (io, repl:: Function , str, r, pattern) =
374
374
print (io, repl (SubString (str, first (r), last (r))))
375
375
376
- # TODO : rename to `replace` when `replace` is removed from deprecated.jl
377
- function replace_new (str :: String , pattern, repl, count :: Integer )
376
+ function replace (str :: String , pat_repl :: Pair ; count :: Integer = typemax (Int))
377
+ pattern, repl = pat_repl
378
378
count == 0 && return str
379
379
count < 0 && throw (DomainError (count, " `count` must be non-negative." ))
380
380
n = 1
@@ -407,11 +407,11 @@ function replace_new(str::String, pattern, repl, count::Integer)
407
407
end
408
408
409
409
"""
410
- replace(s::AbstractString, pat, r, [count::Integer])
410
+ replace(s::AbstractString, pat=>r; [count::Integer])
411
411
412
412
Search for the given pattern `pat` in `s`, and replace each occurrence with `r`.
413
413
If `count` is provided, replace at most `count` occurrences.
414
- As with [`search`](@ref), the second argument may be a
414
+ As with [`search`](@ref), `pat` may be a
415
415
single character, a vector or a set of characters, a string, or a regular expression. If `r`
416
416
is a function, each occurrence is replaced with `r(s)` where `s` is the matched substring.
417
417
If `pat` is a regular expression and `r` is a `SubstitutionString`, then capture group
@@ -420,20 +420,18 @@ To remove instances of `pat` from `string`, set `r` to the empty `String` (`""`)
420
420
421
421
# Examples
422
422
```jldoctest
423
- julia> replace("Python is a programming language.", "Python", "Julia")
423
+ julia> replace("Python is a programming language.", "Python" => "Julia")
424
424
"Julia is a programming language."
425
425
426
- julia> replace("The quick foxes run quickly.", "quick", "slow", 1)
426
+ julia> replace("The quick foxes run quickly.", "quick" => "slow", count= 1)
427
427
"The slow foxes run quickly."
428
428
429
- julia> replace("The quick foxes run quickly.", "quick", "", 1)
429
+ julia> replace("The quick foxes run quickly.", "quick" => "", count= 1)
430
430
"The foxes run quickly."
431
431
```
432
432
"""
433
- replace (s:: AbstractString , pat, f) = replace_new (String (s), pat, f, typemax (Int))
434
- # TODO : change this to the following when `replace` is removed from deprecated.jl:
435
- # replace(s::AbstractString, pat, f, count::Integer=typemax(Int)) =
436
- # replace(String(s), pat, f, count)
433
+ replace (s:: AbstractString , pat_f:: Pair ; count= typemax (Int)) =
434
+ replace (String (s), pat_f, count= count)
437
435
438
436
# TODO : allow transform as the first argument to replace?
439
437
0 commit comments