File tree Expand file tree Collapse file tree 3 files changed +24
-1
lines changed Expand file tree Collapse file tree 3 files changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,7 @@ Standard library changes
47
47
` keep ` that are to be kept as they are. ([ #38597 ] ).
48
48
* ` getindex ` can now be used on ` NamedTuple ` s with multiple values ([ #38878 ] )
49
49
* ` keys(::RegexMatch) ` is now defined to return the capture's keys, by name if named, or by index if not ([ #37299 ] ).
50
+ * ` RegexMatch ` now iterate to give their captures. ([ #34355 ] ).
50
51
51
52
#### Package Manager
52
53
Original file line number Diff line number Diff line change @@ -166,7 +166,7 @@ function show(io::IO, m::RegexMatch)
166
166
for (i, capture_name) in enumerate (capture_keys)
167
167
print (io, capture_name, " =" )
168
168
show (io, m. captures[i])
169
- if i < length (m. captures )
169
+ if i < length (m)
170
170
print (io, " , " )
171
171
end
172
172
end
@@ -190,6 +190,10 @@ function haskey(m::RegexMatch, name::Symbol)
190
190
end
191
191
haskey (m:: RegexMatch , name:: AbstractString ) = haskey (m, Symbol (name))
192
192
193
+ iterate (m:: RegexMatch , args... ) = iterate (m. captures, args... )
194
+ length (m:: RegexMatch ) = length (m. captures)
195
+ eltype (m:: RegexMatch ) = eltype (m. captures)
196
+
193
197
function occursin (r:: Regex , s:: AbstractString ; offset:: Integer = 0 )
194
198
compile (r)
195
199
return PCRE. exec_r (r. regex, String (s), offset, r. match_options)
Original file line number Diff line number Diff line change 167
167
@test r" this|that" ^ 2 == r" (?:this|that){2}"
168
168
end
169
169
170
+ @testset " iterate" begin
171
+ m = match (r" (.) test (.+)" , " a test 123" )
172
+ @test first (m) == " a"
173
+ @test collect (m) == [" a" , " 123" ]
174
+ for (i, capture) in enumerate (m)
175
+ i == 1 && @test capture == " a"
176
+ i == 2 && @test capture == " 123"
177
+ end
178
+ end
179
+
180
+ @testset " Destructuring dispatch" begin
181
+ handle (:: Nothing ) = " not found"
182
+ handle ((capture,):: RegexMatch ) = " found $capture "
183
+
184
+ @test handle (match (r" a (\d )" , " xyz" )) == " not found"
185
+ @test handle (match (r" a (\d )" , " a 1" )) == " found 1"
186
+ end
187
+
170
188
# Test that PCRE throws the correct kind of error
171
189
# TODO : Uncomment this once the corresponding change has propagated to CI
172
190
# @test_throws ErrorException Base.PCRE.info(C_NULL, Base.PCRE.INFO_NAMECOUNT, UInt32)
You can’t perform that action at this time.
0 commit comments