Skip to content
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

add findall(pattern, string) #31834

Merged
merged 2 commits into from
Apr 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Build system changes
New library functions
---------------------

* New `findall(pattern, string)` method where `pattern` is a string or regex ([#31834]).

Standard library changes
------------------------
Expand Down
24 changes: 24 additions & 0 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,30 @@ findnext(r::Regex, s::AbstractString, idx::Integer) = throw(ArgumentError(
))
findfirst(r::Regex, s::AbstractString) = findnext(r,s,firstindex(s))


"""
findall(pattern::Union{AbstractString,Regex}, string::AbstractString; overlap::Bool=false)

Return a `Vector{UnitRange{Int}}` of all the matches for `pattern` in `string`.
Each element of the returned vector is a range of indices where the
matching sequence is found, like the return value of [`findnext`](@ref).

If `overlap=true`, the matching sequences are allowed to overlap indices in the
original string, otherwise they must be from distinct character ranges.
"""
function findall(t::Union{AbstractString,Regex}, s::AbstractString; overlap::Bool=false)
found = UnitRange{Int}[]
i, e = firstindex(s), lastindex(s)
while true
r = findnext(t, s, i)
isnothing(r) && return found
push!(found, r)
j = overlap || isempty(r) ? first(r) : last(r)
j > e && return found
@inbounds i = nextind(s, j)
end
end

"""
SubstitutionString(substr)

Expand Down
6 changes: 6 additions & 0 deletions test/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
@test_throws ArgumentError match(r"test", GenericString("this is a test"))
@test_throws ArgumentError findfirst(r"test", GenericString("this is a test"))

# findall:
@test findall(r"\w+", "foo bar") == [1:3, 5:7]
@test findall(r"\w+", "foo bar", overlap=true) == [1:3, 2:3, 3:3, 5:7, 6:7, 7:7]
@test findall(r"\w*", "foo bar") == [1:3, 4:3, 5:7, 8:7]
@test findall(r"\b", "foo bar") == [1:0, 4:3, 5:4, 8:7]

# Named subpatterns
let m = match(r"(?<a>.)(.)(?<b>.)", "xyz")
@test (m[:a], m[2], m["b"]) == ("x", "y", "z")
Expand Down
10 changes: 10 additions & 0 deletions test/strings/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,13 @@ end
s_18109 = "fooα🐨βcd3"
@test findlast(isequal('o'), s_18109) == 3
@test findfirst(isequal('d'), s_18109) == 13

# findall (issue #31788)
@testset "findall" begin
@test findall("fooo", "foo") == UnitRange{Int}[]
@test findall("ing", "Spinning laughing dancing") == [6:8, 15:17, 23:25]
@test findall("", "foo") == [1:0, 2:1, 3:2, 4:3]
@test findall("αβ", "blαh blαβ blαββy") == findall("αβ", "blαh blαβ blαββy", overlap=true) == [9:11, 16:18]
@test findall("aa", "aaaaaa") == [1:2, 3:4, 5:6]
@test findall("aa", "aaaaaa", overlap=true) == [1:2, 2:3, 3:4, 4:5, 5:6]
end