I like that idea, but it's not really available AFAICT.

The function eachmatch(::Regex, ::String) gives you an iterator over the 
match objects.

In any case, I had a little play with this for fun :) - so here's one way 
to do it:
(n.b. the function arguments don't really need to be typed, I just do it to 
make the code clearer to read):

replacerange(s::AbstractString, replacement::AbstractString, 
range::UnitRange{Int64}) = 
  s[1:range.start-1]*replacement*s[range.stop+1:end]

replacematches(s::AbstractString, r::Regex, f::Function) = 
    foldl((_s,m) -> begin
        offsetd = length(_s) - length(s) #needed because previous replaces 
may change the length of _s
        repl_range = (offsetd + m.offset):(offsetd + m.offset + 
length(m.match)-1)
        replacerange(_s, f(m), repl_range)
    end, s, eachmatch(r,s))

y = "time format 02:45pm, 14:20, 03:45am"
replacematches(y, r"([01]\d)(:[0-6]\d)(am|pm)"i, 
  x->lowercase(x.captures[3])=="am"?x.match[1:end-2]:string(parse(Int, 
x.captures[1])+12, x.captures[2]))

"time format 14:45, 14:20, 03:45"



Reply via email to