Skip to content

Commit c8f18a6

Browse files
committed
Make RegexMatch iterable
don't forget to pass the state add length and eltype to RegexMatch Fix missing enumerate =update news for RegexMatch iterating fix typo in news Co-authored-by: Curtis Vogt <[email protected]> remove example of possible use from NEWS.md Remove stray at-test
1 parent 33573ec commit c8f18a6

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Standard library changes
4646
* `escape_string` can now receive a collection of characters in the keyword
4747
`keep` that are to be kept as they are. ([#38597]).
4848
* `getindex` can now be used on `NamedTuple`s with multiple values ([#38878])
49+
* `RegexMatch` now iterate to give their captures. ([#34355]).
4950

5051
#### Package Manager
5152

base/regex.jl

+7-3
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,13 @@ function show(io::IO, m::RegexMatch)
155155
idx_to_capture_name = PCRE.capture_names(m.regex.regex)
156156
if !isempty(m.captures)
157157
print(io, ", ")
158-
for i = 1:length(m.captures)
158+
for (i, capture) in enumerate(m)
159159
# If the capture group is named, show the name.
160160
# Otherwise show its index.
161161
capture_name = get(idx_to_capture_name, i, i)
162162
print(io, capture_name, "=")
163-
show(io, m.captures[i])
164-
if i < length(m.captures)
163+
show(io, capture)
164+
if i < length(m)
165165
print(io, ", ")
166166
end
167167
end
@@ -185,6 +185,10 @@ function haskey(m::RegexMatch, name::Symbol)
185185
end
186186
haskey(m::RegexMatch, name::AbstractString) = haskey(m, Symbol(name))
187187

188+
iterate(m::RegexMatch, args...) = iterate(m.captures, args...)
189+
length(m::RegexMatch) = length(m.captures)
190+
eltype(m::RegexMatch) = eltype(m.captures)
191+
188192
function occursin(r::Regex, s::AbstractString; offset::Integer=0)
189193
compile(r)
190194
return PCRE.exec_r(r.regex, String(s), offset, r.match_options)

test/regex.jl

+18
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,24 @@
166166
@test r"this|that"^2 == r"(?:this|that){2}"
167167
end
168168

169+
@testset "iterate" begin
170+
m = match(r"(.) test (.+)", "a test 123")
171+
@test first(m) == "a"
172+
@test collect(m) == ["a", "123"]
173+
for (i, capture) in enumerate(m)
174+
i == 1 && @test capture == "a"
175+
i == 2 && @test capture == "123"
176+
end
177+
end
178+
179+
@testset "Destructuring dispatch" begin
180+
handle(::Nothing) = "not found"
181+
handle((capture,)::RegexMatch) = "found $capture"
182+
183+
@test handle(match(r"a (\d)", "xyz")) == "not found"
184+
@test handle(match(r"a (\d)", "a 1")) == "found 1"
185+
end
186+
169187
# Test that PCRE throws the correct kind of error
170188
# TODO: Uncomment this once the corresponding change has propagated to CI
171189
#@test_throws ErrorException Base.PCRE.info(C_NULL, Base.PCRE.INFO_NAMECOUNT, UInt32)

0 commit comments

Comments
 (0)