Skip to content

Commit 6cd052b

Browse files
committed
Deprecate ismatch(r, s) in favor of contains(s, r)
1 parent a2bfbc7 commit 6cd052b

38 files changed

+118
-123
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,8 @@ Deprecated or removed
901901
`findlast`/`findprev` respectively, in combination with the new `equalto` and `occursin`
902902
predicates for some methods ([#24673]
903903

904+
* `ismatch(regex, str)` has been deprecated in favor of `contains(str, regex)` ([#24673]).
905+
904906
Command-line option changes
905907
---------------------------
906908

base/deprecated.jl

+2
Original file line numberDiff line numberDiff line change
@@ -3854,6 +3854,8 @@ end
38543854
@deprecate rsearchindex(s::AbstractString, c::Char) first(findlast(equalto(c), s))
38553855
@deprecate rsearchindex(s::AbstractString, c::Char, i::Integer) first(findprev(equalto(c), s, i))
38563856

3857+
@deprecate ismatch(r::Regex, s::AbstractString) contains(s, r)
3858+
38573859
# END 0.7 deprecations
38583860

38593861
# BEGIN 1.0 deprecations

base/docs/utils.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ const builtins = ["abstract type", "baremodule", "begin", "break",
359359

360360
moduleusings(mod) = ccall(:jl_module_usings, Any, (Any,), mod)
361361

362-
filtervalid(names) = filter(x->!ismatch(r"#", x), map(string, names))
362+
filtervalid(names) = filter(x->!contains(x, r"#"), map(string, names))
363363

364364
accessible(mod::Module) =
365365
[filter!(s -> !Base.isdeprecated(mod, s), names(mod, true, true));

base/exports.jl

-1
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,6 @@ export
503503
findnext,
504504
findprev,
505505
findnz,
506-
ismatch,
507506
occursin,
508507
match,
509508
matchall,

base/interactiveutil.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,13 @@ function versioninfo(io::IO=STDOUT; verbose::Bool=false, packages::Bool=false)
334334

335335
println(io, "Environment:")
336336
for (k,v) in ENV
337-
if ismatch(r"JULIA", String(k))
337+
if contains(String(k), r"JULIA")
338338
println(io, " $(k) = $(v)")
339339
end
340340
end
341341
if verbose
342342
for (k,v) in ENV
343-
if ismatch(r"PATH|FLAG|^TERM$|HOME", String(k))
343+
if contains(String(k), r"PATH|FLAG|^TERM$|HOME")
344344
println(io, " $(k) = $(v)")
345345
end
346346
end
@@ -737,7 +737,7 @@ function varinfo(m::Module=Main, pattern::Regex=r"")
737737
(value (Base, Main, Core) ? "" : format_bytes(summarysize(value))),
738738
summary(value)]
739739
end
740-
for v in sort!(names(m)) if isdefined(m, v) && ismatch(pattern, string(v)) ]
740+
for v in sort!(names(m)) if isdefined(m, v) && contains(string(v), pattern) ]
741741

742742
pushfirst!(rows, Any["name", "size", "summary"])
743743

base/libc.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ function strptime(fmt::AbstractString, timestr::AbstractString)
204204
@static if Sys.isapple()
205205
# if we didn't explicitly parse the weekday or year day, use mktime
206206
# to fill them in automatically.
207-
if !ismatch(r"([^%]|^)%(a|A|j|w|Ow)", fmt)
207+
if !contains(fmt, r"([^%]|^)%(a|A|j|w|Ow)")
208208
ccall(:mktime, Int, (Ref{TmStruct},), tm)
209209
end
210210
end

base/linalg/blas.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function check()
122122
blas = vendor()
123123
if blas == :openblas || blas == :openblas64
124124
openblas_config = openblas_get_config()
125-
openblas64 = ismatch(r".*USE64BITINT.*", openblas_config)
125+
openblas64 = contains(openblas_config, r".*USE64BITINT.*")
126126
if Base.USE_BLAS64 != openblas64
127127
if !openblas64
128128
@error """

base/linalg/uniformscaling.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ getindex(J::UniformScaling, i::Integer,j::Integer) = ifelse(i==j,J.λ,zero(J.λ)
5555

5656
function show(io::IO, J::UniformScaling)
5757
s = "$(J.λ)"
58-
if ismatch(r"\w+\s*[\+\-]\s*\w+", s)
58+
if contains(s, r"\w+\s*[\+\-]\s*\w+")
5959
s = "($s)"
6060
end
6161
print(io, "$(typeof(J))\n$s*I")

base/markdown/Common/block.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,11 @@ function admonition(stream::IO, block::MD)
211211
let untitled = r"^([a-z]+)$", # !!! <CATEGORY_NAME>
212212
titled = r"^([a-z]+) \"(.*)\"$", # !!! <CATEGORY_NAME> "<TITLE>"
213213
line = strip(readline(stream))
214-
if ismatch(untitled, line)
214+
if contains(line, untitled)
215215
m = match(untitled, line)
216216
# When no title is provided we use CATEGORY_NAME, capitalising it.
217217
m.captures[1], ucfirst(m.captures[1])
218-
elseif ismatch(titled, line)
218+
elseif contains(line, titled)
219219
m = match(titled, line)
220220
# To have a blank TITLE provide an explicit empty string as TITLE.
221221
m.captures[1], m.captures[2]
@@ -270,10 +270,10 @@ function list(stream::IO, block::MD)
270270
indent = isempty(bullet) ? (return false) : length(bullet)
271271
# Calculate the starting number and regex to use for bullet matching.
272272
initial, regex =
273-
if ismatch(BULLETS, bullet)
273+
if contains(bullet, BULLETS)
274274
# An unordered list. Use `-1` to flag the list as unordered.
275275
-1, BULLETS
276-
elseif ismatch(r"^ {0,3}\d+(\.|\))( |$)", bullet)
276+
elseif contains(bullet, r"^ {0,3}\d+(\.|\))( |$)")
277277
# An ordered list. Either with `1. ` or `1) ` style numbering.
278278
r = contains(bullet, ".") ? r"^ {0,3}(\d+)\.( |$)" : r"^ {0,3}(\d+)\)( |$)"
279279
Base.parse(Int, match(r, bullet).captures[1]), r

base/markdown/Common/inline.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function _is_mailto(s::AbstractString)
140140
length(s) < 6 && return false
141141
# slicing strings is a bit risky, but this equality check is safe
142142
lowercase(s[1:6]) == "mailto:" || return false
143-
return ismatch(_email_regex, s[6:end])
143+
return contains(s[6:end], _email_regex)
144144
end
145145

146146
# –––––––––––

base/markdown/render/rst.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ rstinline(io::IO, md::Vector) = !isempty(md) && rstinline(io, md...)
115115
# rstinline(io::IO, md::Image) = rstinline(io, ".. image:: ", md.url)
116116

117117
function rstinline(io::IO, md::Link)
118-
if ismatch(r":(func|obj|ref|exc|class|const|data):`\.*", md.url)
118+
if contains(md.url, r":(func|obj|ref|exc|class|const|data):`\.*")
119119
rstinline(io, md.url)
120120
else
121121
rstinline(io, "`", md.text, " <", md.url, ">`_")

base/markdown/render/terminal/formatting.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ lines(s) = split(s, "\n")
5959

6060
# This could really be more efficient
6161
function wrapped_lines(io::IO, s::AbstractString; width = 80, i = 0)
62-
if ismatch(r"\n", s)
62+
if contains(s, r"\n")
6363
return vcat(map(s->wrapped_lines(io, s, width = width, i = i), split(s, "\n"))...)
6464
end
6565
ws = words(s)

base/methodshow.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ function url(m::Method)
202202
(m.file == :null || m.file == :string) && return ""
203203
file = string(m.file)
204204
line = m.line
205-
line <= 0 || ismatch(r"In\[[0-9]+\]", file) && return ""
205+
line <= 0 || contains(file, r"In\[[0-9]+\]") && return ""
206206
Sys.iswindows() && (file = replace(file, '\\' => '/'))
207207
if inbase(M)
208208
if isempty(Base.GIT_VERSION_INFO.commit)

base/path.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ end
7878

7979

8080
if Sys.iswindows()
81-
isabspath(path::String) = ismatch(path_absolute_re, path)
81+
isabspath(path::String) = contains(path, path_absolute_re)
8282
else
8383
isabspath(path::String) = startswith(path, '/')
8484
end
@@ -113,7 +113,7 @@ julia> isdirpath("/home/")
113113
true
114114
```
115115
"""
116-
isdirpath(path::String) = ismatch(path_directory_re, splitdrive(path)[2])
116+
isdirpath(path::String) = contains(splitdrive(path)[2], path_directory_re)
117117

118118
"""
119119
splitdir(path::AbstractString) -> (AbstractString, AbstractString)
@@ -218,9 +218,9 @@ function joinpath(a::String, b::String)
218218
B, b = splitdrive(b)
219219
!isempty(B) && A != B && return string(B,b)
220220
C = isempty(B) ? A : B
221-
isempty(a) ? string(C,b) :
222-
ismatch(path_separator_re, a[end:end]) ? string(C,a,b) :
223-
string(C,a,pathsep(a,b),b)
221+
isempty(a) ? string(C,b) :
222+
contains(a[end:end], path_separator_re) ? string(C,a,b) :
223+
string(C,a,pathsep(a,b),b)
224224
end
225225
joinpath(a::AbstractString, b::AbstractString) = joinpath(String(a), String(b))
226226

base/pkg/read.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function available(names=readdir("METADATA"))
1818
versdir = joinpath("METADATA", pkg, "versions")
1919
isdir(versdir) || continue
2020
for ver in readdir(versdir)
21-
ismatch(Base.VERSION_REGEX, ver) || continue
21+
contains(ver, Base.VERSION_REGEX) || continue
2222
isfile(versdir, ver, "sha1") || continue
2323
haskey(pkgs,pkg) || (pkgs[pkg] = Dict{VersionNumber,Available}())
2424
pkgs[pkg][VersionNumber(ver)] = Available(
@@ -39,7 +39,7 @@ function latest(names=readdir("METADATA"))
3939
isdir(versdir) || continue
4040
pkgversions = VersionNumber[]
4141
for ver in readdir(versdir)
42-
ismatch(Base.VERSION_REGEX, ver) || continue
42+
contains(ver, Base.VERSION_REGEX) || continue
4343
isfile(versdir, ver, "sha1") || continue
4444
push!(pkgversions, VersionNumber(ver))
4545
end
@@ -114,7 +114,7 @@ function ispinned(prepo::LibGit2.GitRepo)
114114
LibGit2.isattached(prepo) || return false
115115
br = LibGit2.branch(prepo)
116116
# note: regex is based on the naming scheme used in Entry.pin()
117-
return ismatch(r"^pinned\.[0-9a-f]{8}\.tmp$", br)
117+
return contains(br, r"^pinned\.[0-9a-f]{8}\.tmp$")
118118
end
119119

120120
function installed_version(pkg::AbstractString, prepo::LibGit2.GitRepo, avail::Dict=available(pkg))

base/pkg/reqs.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct Requirement <: Line
2626
end
2727
isempty(fields) && throw(PkgError("invalid requires entry: $content"))
2828
package = popfirst!(fields)
29-
all(field->ismatch(Base.VERSION_REGEX, field), fields) ||
29+
all(field->contains(field, Base.VERSION_REGEX), fields) ||
3030
throw(PkgError("invalid requires entry for $package: $content"))
3131
versions = map(VersionNumber, fields)
3232
issorted(versions) || throw(PkgError("invalid requires entry for $package: $content"))
@@ -58,15 +58,15 @@ function read(readable::Vector{<:AbstractString})
5858
lines = Line[]
5959
for line in readable
6060
line = chomp(line)
61-
push!(lines, ismatch(r"^\s*(?:#|$)", line) ? Comment(line) : Requirement(line))
61+
push!(lines, contains(line, r"^\s*(?:#|$)") ? Comment(line) : Requirement(line))
6262
end
6363
return lines
6464
end
6565

6666
function read(readable::Union{IO,Base.AbstractCmd})
6767
lines = Line[]
6868
for line in eachline(readable)
69-
push!(lines, ismatch(r"^\s*(?:#|$)", line) ? Comment(line) : Requirement(line))
69+
push!(lines, contains(line, r"^\s*(?:#|$)") ? Comment(line) : Requirement(line))
7070
end
7171
return lines
7272
end

base/random/misc.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ let groupings = [1:8; 10:13; 15:18; 20:23; 25:36]
432432
function UUID(s::AbstractString)
433433
s = Base.Unicode.lowercase(s)
434434

435-
if !ismatch(r"^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$", s)
435+
if !contains(s, r"^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$")
436436
throw(ArgumentError("Malformed UUID string"))
437437
end
438438

base/regex.jl

+3-23
Original file line numberDiff line numberDiff line change
@@ -141,39 +141,19 @@ function getindex(m::RegexMatch, name::Symbol)
141141
end
142142
getindex(m::RegexMatch, name::AbstractString) = m[Symbol(name)]
143143

144-
"""
145-
ismatch(r::Regex, s::AbstractString) -> Bool
146-
147-
Test whether a string contains a match of the given regular expression.
148-
149-
# Examples
150-
```jldoctest
151-
julia> rx = r"a.a"
152-
r"a.a"
153-
154-
julia> ismatch(rx, "aba")
155-
true
156-
157-
julia> ismatch(rx, "abba")
158-
false
159-
160-
julia> rx("aba")
161-
true
162-
```
163-
"""
164-
function ismatch(r::Regex, s::AbstractString, offset::Integer=0)
144+
function contains(s::AbstractString, r::Regex, offset::Integer=0)
165145
compile(r)
166146
return PCRE.exec(r.regex, String(s), offset, r.match_options,
167147
r.match_data)
168148
end
169149

170-
function ismatch(r::Regex, s::SubString, offset::Integer=0)
150+
function contains(s::SubString, r::Regex, offset::Integer=0)
171151
compile(r)
172152
return PCRE.exec(r.regex, s, offset, r.match_options,
173153
r.match_data)
174154
end
175155

176-
(r::Regex)(s) = ismatch(r, s)
156+
(r::Regex)(s) = contains(s, r)
177157

178158
"""
179159
match(r::Regex, s::AbstractString[, idx::Integer[, addopts]])

base/repl/REPLCompletions.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function complete_keyword(s::Union{String,SubString{String}})
116116
end
117117

118118
function complete_path(path::AbstractString, pos; use_envpath=false)
119-
if Base.Sys.isunix() && ismatch(r"^~(?:/|$)", path)
119+
if Base.Sys.isunix() && contains(path, r"^~(?:/|$)")
120120
# if the path is just "~", don't consider the expanded username as a prefix
121121
if path == "~"
122122
dir, prefix = homedir(), ""
@@ -409,7 +409,7 @@ function afterusing(string::String, startpos::Int)
409409
r = findfirst(r"\s(gnisu|tropmi)\b", rstr)
410410
isempty(r) && return false
411411
fr = reverseind(str, last(r))
412-
return ismatch(r"^\b(using|import)\s*((\w+[.])*\w+\s*,\s*)*$", str[fr:end])
412+
return contains(str[fr:end], r"^\b(using|import)\s*((\w+[.])*\w+\s*,\s*)*$")
413413
end
414414

415415
function bslash_completions(string, pos)

base/repl/latex_symbols.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ for c in child_nodes(root(xdoc))
2424
id = attribute(ce, "id")
2525
U = string(map(s -> Char(parse(Int, s, 16)),
2626
split(id[2:end], "-"))...)
27-
if ismatch(r"^\\[A-Za-z]+$",L) && !isa(U,String)
27+
if contains(L, r"^\\[A-Za-z]+$") && !isa(U,String)
2828
if L in Ls
2929
println("# duplicated symbol $L ($id)")
3030
else

base/strings/search.jl

+14-2
Original file line numberDiff line numberDiff line change
@@ -448,16 +448,28 @@ julia> findprev("Julia", "JuliaLang", 6)
448448
findprev(t::AbstractString, s::AbstractString, i::Integer) = _rsearch(s, t, i)
449449

450450
"""
451-
contains(haystack::AbstractString, needle::Union{AbstractString,Char})
451+
contains(haystack::AbstractString, needle::Union{AbstractString,Regex,Char})
452452
453-
Determine whether the second argument is a substring of the first.
453+
Determine whether the second argument is a substring of the first. If `needle`
454+
is a regular expression, checks whether `haystack` contains a match.
454455
455456
# Examples
456457
```jldoctest
457458
julia> contains("JuliaLang is pretty cool!", "Julia")
458459
true
460+
461+
julia> contains("JuliaLang is pretty cool!", 'a')
462+
true
463+
464+
julia> contains("aba", r"a.a")
465+
true
466+
467+
julia> contains("abba", r"a.a")
468+
false
459469
```
460470
"""
471+
function contains end
472+
461473
contains(haystack::AbstractString, needle::Union{AbstractString,Char}) =
462474
_searchindex(haystack, needle, start(haystack)) != 0
463475

base/version.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct VersionNumber
2121
if ident isa Integer
2222
ident >= 0 || throw(ArgumentError("invalid negative pre-release identifier: $ident"))
2323
else
24-
if !ismatch(r"^(?:|[0-9a-z-]*[a-z-][0-9a-z-]*)$"i, ident) ||
24+
if !contains(ident, r"^(?:|[0-9a-z-]*[a-z-][0-9a-z-]*)$"i) ||
2525
isempty(ident) && !(length(pre)==1 && isempty(bld))
2626
throw(ArgumentError("invalid pre-release identifier: $(repr(ident))"))
2727
end
@@ -31,7 +31,7 @@ struct VersionNumber
3131
if ident isa Integer
3232
ident >= 0 || throw(ArgumentError("invalid negative build identifier: $ident"))
3333
else
34-
if !ismatch(r"^(?:|[0-9a-z-]*[a-z-][0-9a-z-]*)$"i, ident) ||
34+
if !contains(ident, r"^(?:|[0-9a-z-]*[a-z-][0-9a-z-]*)$"i) ||
3535
isempty(ident) && length(bld)!=1
3636
throw(ArgumentError("invalid build identifier: $(repr(ident))"))
3737
end
@@ -83,7 +83,7 @@ function split_idents(s::AbstractString)
8383
idents = split(s, '.')
8484
ntuple(length(idents)) do i
8585
ident = idents[i]
86-
ismatch(r"^\d+$", ident) ? parse(UInt64, ident) : String(ident)
86+
contains(ident, r"^\d+$") ? parse(UInt64, ident) : String(ident)
8787
end
8888
end
8989

doc/src/manual/strings.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -604,17 +604,17 @@ julia> typeof(ans)
604604
Regex
605605
```
606606

607-
To check if a regex matches a string, use [`ismatch`](@ref):
607+
To check if a regex matches a string, use [`contains`](@ref):
608608

609609
```jldoctest
610-
julia> ismatch(r"^\s*(?:#|$)", "not a comment")
610+
julia> contains("not a comment", r"^\s*(?:#|$)")
611611
false
612612
613-
julia> ismatch(r"^\s*(?:#|$)", "# a comment")
613+
julia> contains("# a comment", r"^\s*(?:#|$)")
614614
true
615615
```
616616

617-
As one can see here, [`ismatch`](@ref) simply returns true or false, indicating whether the
617+
As one can see here, [`contains`](@ref) simply returns true or false, indicating whether the
618618
given regex matches the string or not. Commonly, however, one wants to know not just whether a
619619
string matched, but also *how* it matched. To capture this information about a match, use the
620620
[`match`](@ref) function instead:

0 commit comments

Comments
 (0)