replace(*string*, *pat*, *r*[, *n*])

Search for the given pattern pat, and replace each occurrence with r. If n 
is provided, replace at most n occurrences. As with search, the second 
argument may be a single character, a vector or a set of characters, a 
string, or a regular expression. If r is a function, each occurrence is 
replaced with r(s) where s is the matched substring.


               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "help()" to list help topics
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.2.0 (2013-11-16 23:44 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org release
|__/                   |  i686-w64-mingw32

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

julia> replace(originalstring,r"\b([\w]*e) ([\w]*)","not \1 black \2")
"Mary had a not \x01 black \x02, her skin is as not \x01 black \x02 snow"



On Wednesday, August 13, 2014 4:05:23 PM UTC+10, John Myles White wrote:
>
> Suspect I'm missing something. How is this different from the existing 
> function replace? 
>
>  -- John 
>
> On Aug 12, 2014, at 11:03 PM, Steven Siew <[email protected] 
> <javascript:>> wrote: 
>
> > 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