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

Make startswith, endswith work with Regex #29790

Merged
merged 17 commits into from
Feb 1, 2019
1 change: 1 addition & 0 deletions base/pcre.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const COMPILE_MASK =
CASELESS |
DOLLAR_ENDONLY |
DOTALL |
ENDANCHORED |
EXTENDED |
FIRSTLINE |
MULTILINE |
Expand Down
58 changes: 58 additions & 0 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,64 @@ function occursin(r::Regex, s::SubString; offset::Integer=0)
r.match_data)
end

"""
startswith(s::AbstractString, prefix::Regex)

Return `true` if `s` starts with the regex pattern, `prefix`.

!!! note
`occursin` is recommended over the regex version of `startswith`
in performance critical situations.
dalum marked this conversation as resolved.
Show resolved Hide resolved

See also [`occursin`](@ref) and [`endswith`](@ref).

# Examples
```jldoctest
julia> startswith("JuliaLang", r"Julia|Romeo")
true
```
"""
function startswith(s::AbstractString, r::Regex)
dalum marked this conversation as resolved.
Show resolved Hide resolved
compile(r)
return PCRE.exec(r.regex, String(s), 0, r.match_options | PCRE.ANCHORED,
r.match_data)
end

function startswith(s::SubString, r::Regex)
compile(r)
return PCRE.exec(r.regex, s, 0, r.match_options | PCRE.ANCHORED,
r.match_data)
end

"""
endswith(s::AbstractString, suffix::Regex)

Return `true` if `s` ends with the regex pattern, `suffix`.

!!! note
`occursin` is recommended over the regex version of `endswith` in
performance critical situations.

See also [`occursin`](@ref) and [`startswith`](@ref).

# Examples
```jldoctest
julia> endswith("JuliaLang", r"Lang|Roberts")
true
```
"""
function endswith(s::AbstractString, r::Regex)
dalum marked this conversation as resolved.
Show resolved Hide resolved
compile(r)
return PCRE.exec(r.regex, String(s), 0, r.match_options | PCRE.ENDANCHORED,
r.match_data)
end

function endswith(s::SubString, r::Regex)
compile(r)
return PCRE.exec(r.regex, s, 0, r.match_options | PCRE.ENDANCHORED,
r.match_data)
end

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

Expand Down
12 changes: 12 additions & 0 deletions test/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,15 @@ end
# 'a' flag to disable UCP
@test match(r"\w+", "Düsseldorf").match == "Düsseldorf"
@test match(r"\w+"a, "Düsseldorf").match == "D"

@test startswith("abc", r"a")
@test endswith("abc", r"c")
dalum marked this conversation as resolved.
Show resolved Hide resolved
@test !startswith("abc", r"b")
@test !startswith("abc", r"c")
@test !endswith("abc", r"a")
@test !endswith("abc", r"b")

@test !startswith("abc", r"A")
@test startswith("abc", r"A"i)
@test !endswith("abc", r"C")
@test endswith("abc", r"C"i)