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

match_all function to match all occurrences of a regex pattern in a string #3525

Closed
wants to merge 2 commits into from
Closed
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 base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ export
lpad,
lstrip,
match,
match_all,
ismatch,
nextind,
prevind,
Expand Down
12 changes: 12 additions & 0 deletions base/pcre.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ function info{T}(
reinterpret(T,buf)[1]
end

function config{T}(what::Integer, ::Type{T})
buf = Array(Uint8, sizeof(T))
ret = ccall((:pcre_config, :libpcre), Int32,
(Int32, Ptr{Uint8}),
what, buf)

if ret != 0
error("config: error $n")
end
reinterpret(T,buf)[1]
end

function compile(pattern::String, options::Integer)
errstr = Array(Ptr{Uint8},1)
erroff = Array(Int32,1)
Expand Down
49 changes: 47 additions & 2 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ end
ismatch(r::Regex, s::String) =
PCRE.exec(r.regex, C_NULL, bytestring(s), 0, r.options & PCRE.EXECUTE_MASK, false)

function match(re::Regex, str::ByteString, idx::Integer)
opts = re.options & PCRE.EXECUTE_MASK
function match(re::Regex, str::ByteString, idx::Integer, add_opts::Uint32)
opts = re.options & PCRE.EXECUTE_MASK | add_opts
m, n = PCRE.exec(re.regex, C_NULL, str, idx-1, opts, true)
if isempty(m); return nothing; end
mat = str[m[1]+1:m[2]]
Expand All @@ -95,10 +95,55 @@ function match(re::Regex, str::ByteString, idx::Integer)
off = Int[ m[2i+1]::Int32+1 for i=1:n ]
RegexMatch(mat, cap, m[1]+1, off)
end
match(re::Regex, str::ByteString, idx::Integer) = match(re, str, idx, uint32(0))
match(r::Regex, s::String) = match(r, s, start(s))
match(r::Regex, s::String, i::Integer) =
error("regex matching is only available for bytestrings; use bytestring(s) to convert")

function match_all(re::Regex, str::ByteString)
m = match(re, str, 1)
if m == nothing; return nothing; end

re_opts = PCRE.info(re.regex, C_NULL, PCRE.INFO_OPTIONS, Uint32)
is_utf = (re_opts & PCRE.UTF8) != 0

matches = [m]
while true
opts = uint32(0)
if m != nothing
idx = m.offset + length(m.match.data)

if length(m.match) == 0
if m.offset == length(str.data) + 1
break
end
opts = opts | PCRE.ANCHORED | PCRE.NOTEMPTY_ATSTART
end
end

m = match(re, str, idx, opts)
if m == nothing
if opts == 0
break
end
idx += 1
if is_utf
# Ensure we skip entire UTF character.
while idx <= length(str.data)
if (str.data[idx] & 0xc0) != 0x80
break;
end
idx += 1
end
end
continue
end

push!(matches, m)
end
matches
end

function search(str::ByteString, re::Regex, idx::Integer)
len = length(str.data)
if idx >= len+2
Expand Down