Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Using replace with a function of the match

$
0
0

Hello,

It’s easy to recuperate the matched substring in replace and do something like:

julia> replace("foobar", r"b([a-z])r" => s"\1")
"fooa"

However I can’t pass the output to a function so for instance this:

julia> replace("foobar", r"b([a-z])r" => uppercase(s"\1"))
"foo\\1"

this works but is a bit silly with the double matching:

julia> rx = r"b([a-z])r"
julia> replace("foobar", rx => s -> uppercase(match(rx, s).captures[1])
"fooA"

is there an obvious way to do this better? generally what might be nice is to do something like:

replace("foobar", regex => myfun) 

where the myfun would get access to the regex match and have access to fields like captures

myfun(m) = uppercase(m.captures[1])

Thanks!

cc: @Wikunia

4 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795