Using regex like in the programming language Perl to perform replacements.

function perlRegexReplace(str::ASCIIString,regex::Regex,rep::ASCIIString)
  local m,len,result,inter
  m = match(regex,str)
  len=length(m.captures)
  result = replace(str,regex,rep)
  if len > 0
    inter = eachmatch(regex,str)
    for (m in inter)
      for k = 1:len
        # Comment out the next line when using function for production
        println("Replacing \\",k," with ",m.captures[k])
        result = replace(result,char(k),m.captures[k],1)
      end
    end
  end
  result
end

originalstring="Mary had a little lamb, her skin is as white as snow"

resultstring = perlRegexReplace(originalstring,r"\b([\w]*e) ([\w]*)","not 
\1 black \2")

println("ORIGINAL: ",originalstring)
println("RESULTST: ",resultstring)


======================================


C:\oracle\julia\scripts> ..\julia.bat perlRegexReplace3.jl
Replacing \1 with little
Replacing \2 with lamb
Replacing \1 with white
Replacing \2 with as
ORIGINAL: Mary had a little lamb, her skin is as white as snow
RESULTST: Mary had a not little black lamb, her skin is as not white black 
as snow

Reply via email to