José Romildo Malaquias | 18 Aug 14:18 2012
Picon

Regex string replacement

Hello.

I am looking for a substring replacement based on Perl like regular
expressions that would let me use part of the original string in the
replacement string. Something like:

   reReplace "name is (.*)\\." "hi \1!" "my name is john."

which would result in

   "hy john!"

Any help?

Romildo
Picon

Re: Regex string replacement

Hi Romildo.

On Sat, Aug 18, 2012 at 9:18 AM, José Romildo Malaquias <j.romildo <at> gmail.com>
wrote:
(...)
> I am looking for a substring replacement based on Perl like regular
> expressions that would let me use part of the original string in the
> replacement string. Something like:
>
>    reReplace "name is (.*)\\." "hi \1!" "my name is john."
>
> which would result in
>
>    "hy john!"

It's not exactly what you're looking for, but I'd simply do something like this,
using Text.Regex.TDFA or any other:

let [[_, name]] = "my name is john." =~ "name is (.*)\\." in "hi " ++
name ++ "!"

Greetings.
(...)
--

-- 
marcot
http://marcot.eti.br/

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
(Continue reading)

José Romildo Malaquias | 18 Aug 16:23 2012
Picon

Re: Regex string replacement

On Sat, Aug 18, 2012 at 09:39:01AM -0300, Marco Túlio Pimenta Gontijo wrote:
> Hi Romildo.
> 
> On Sat, Aug 18, 2012 at 9:18 AM, José Romildo Malaquias <j.romildo <at> gmail.com>
> wrote:
> (...)
> > I am looking for a substring replacement based on Perl like regular
> > expressions that would let me use part of the original string in the
> > replacement string. Something like:
> >
> >    reReplace "name is (.*)\\." "hi \1!" "my name is john."
> >
> > which would result in
> >
> >    "hy john!"
> 
> It's not exactly what you're looking for, but I'd simply do something like this,
> using Text.Regex.TDFA or any other:
> 
> let [[_, name]] = "my name is john." =~ "name is (.*)\\." in "hi " ++
> name ++ "!"

Thanks for the tip. It was useful to solve my original problem.

Romildo

Gmane