#Version 4
#More optimization done
function perlRegexReplace(str::ASCIIString,regex::Regex,rep::ASCIIString)
local m,result,inter
result = replace(str,regex,rep)
inter = eachmatch(regex,str)
for (m in inter)
for k = 1:length(m.captures)
result = replace(result,char(k),m.captures[k],1)
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)
On Wednesday, August 13, 2014 8:19:08 PM UTC+10, Kevin Squire wrote:
>
> I implemented something related to this a long time ago (
> https://github.com/JuliaLang/julia/pull/1448). At the time, Stefan
> agreed that something like it was needed, but didn't like my
> implementation. Still hasn't happened (other things have had greater
> priority), but I think it would be worthwhile.
>
> Kevin
>
>
> On Wed, Aug 13, 2014 at 12:07 AM, Jameson Nash <[email protected]
> <javascript:>> wrote:
>
>> right, in other words, it implements backreference substitutions
>>
>>
>> On Wed, Aug 13, 2014 at 3:01 AM, Steven Siew <[email protected]
>> <javascript:>> wrote:
>>
>>> 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]> 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
>>>>
>>>>
>>
>