Replace sub-string

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Replace sub-string

Freeman Gilmore
Is there a procedure, to replace 'all' occurrences of a sub-string
within a string. with a string?
If so please give an example.

Thank you, ƒg

Reply | Threaded
Open this post in threaded view
|

Re: Replace sub-string

Aaron Hill
On 2020-05-22 8:38 pm, Freeman Gilmore wrote:
> Is there a procedure, to replace 'all' occurrences of a sub-string
> within a string. with a string?
> If so please give an example.

regexp-substitute/global [1] should do the job.

[1]:
https://www.gnu.org/software/guile/docs/docs-1.8/guile-ref/Regexp-Functions.html#Regexp-Functions

;;;;

(regexp-substitute/global #f
   "(c|sh|w)ould of"
   "I would of done that if I could of."
   'pre 1 "ould have" 'post)

=> "I would have done that if I could have."

;;;;


-- Aaron Hill

Reply | Threaded
Open this post in threaded view
|

Re: Replace sub-string

David Pirotte
Hello,

> On 2020-05-22 8:38 pm, Freeman Gilmore wrote:
> > Is there a procedure, to replace 'all' occurrences of a sub-string
> > within a string. with a string?
> > If so please give an example.  
 
> regexp-substitute/global [1] should do the job.
 
> [1]:
> https://www.gnu.org/software/guile/docs/docs-1.8/guile-ref/Regexp-Functions.html#Regexp-Functions
 
> ;;;;
 
> (regexp-substitute/global #f
>    "(c|sh|w)ould of"
>    "I would of done that if I could of."
>    'pre 1 "ould have" 'post)
> ...

Fwiw, I wrote a none regex version, inspired by a similar code I found
in guix, it is here [1], feel free to snarf it ... Guile 3.0 also has a
version, in (ice-9 string-fun), a complete diff implementation [2] ...

David

[1] Grip - string-replace-all

        http://git.savannah.nongnu.org/cgit/grip.git/tree/grip/string.scm

[2] Guile 3.0 - string-replace-substring

        http://git.savannah.gnu.org/cgit/guile.git/tree/module/ice-9/string-fun.scm?h=master

attachment0 (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Replace sub-string

Freeman Gilmore
In reply to this post by Aaron Hill
Arron:

Just getting to this and having a problem.   See below.

On Sat, May 23, 2020 at 12:31 AM Aaron Hill <[hidden email]> wrote:

>
> On 2020-05-22 8:38 pm, Freeman Gilmore wrote:
> > Is there a procedure, to replace 'all' occurrences of a sub-string
> > within a string. with a string?
> > If so please give an example.
>
> regexp-substitute/global [1] should do the job.
>
> [1]:
> https://www.gnu.org/software/guile/docs/docs-1.8/guile-ref/Regexp-Functions.html#Regexp-Functions
>
> ;;;;
>
> (regexp-substitute/global #f
>    "(c|sh|w)ould of"
>    "I would of done that if I could of."
>    'pre 1 "ould have" 'post)
>
> => "I would have done that if I could have."

\version "2.20.0"
#(regexp-substitute/global #f
   "(c|sh|w)ould of"
   "I would of done that if I could of."
   'pre 1 "ould have" 'post)

Gives:  Unbound variable: regexp-substitute/global

Also:
\version "2.20.0"
#(regexp-substitute/global #f "[ \t]+" "this   is   the test" 'pre "-" 'post)

Gives:  Unbound variable: regexp-substitute/global

What am i missing?

Thank you, ƒg


>
> ;;;;
>
>
> -- Aaron Hill
>

Reply | Threaded
Open this post in threaded view
|

Re: Replace sub-string

Aaron Hill
On 2020-05-28 4:46 am, Freeman Gilmore wrote:
> Just getting to this and having a problem.   See below.

> \version "2.20.0"
> #(regexp-substitute/global #f "[ \t]+" "this   is   the test" 'pre "-"
> 'post)
>
> Gives:  Unbound variable: regexp-substitute/global
>
> What am i missing?

You will need to bring in the regular expression module:

%%%%
\version "2.20.0"

#(use-modules (ice-9 regex))

\markup
   #(regexp-substitute/global #f
     "[ \t]+"
     "this   is   the test"
     'pre "-" 'post)
%%%%

NOTE: I am using \markup above as a simple way to visualize the
resulting string.


-- Aaron Hill

Reply | Threaded
Open this post in threaded view
|

Re: Replace sub-string

Freeman Gilmore
On Thu, May 28, 2020 at 8:10 AM Aaron Hill <[hidden email]> wrote:

>
> On 2020-05-28 4:46 am, Freeman Gilmore wrote:
> > Just getting to this and having a problem.   See below.
>
> > \version "2.20.0"
> > #(regexp-substitute/global #f "[ \t]+" "this   is   the test" 'pre "-"
> > 'post)
> >
> > Gives:  Unbound variable: regexp-substitute/global
> >
> > What am i missing?
>
> You will need to bring in the regular expression module:
>
> %%%%
> \version "2.20.0"
>
> #(use-modules (ice-9 regex))
>
> \markup
>    #(regexp-substitute/global #f
>      "[ \t]+"
>      "this   is   the test"
>      'pre "-" 'post)
> %%%%
Thank you, that works.   Do you normally places '#(use-modules (ice-9
regex))' with the include files?
>
> NOTE: I am using \markup above as a simple way to visualize the
> resulting string.
I did not know about using this, this way, thanks
>
>
> -- Aaron Hill