Topics

String Replace Backwards


Axel Berger
 

Unless I'm very wrong and miss the obvious, ^$StrReplace()$ has a serious
limitation compared to ^!Replace, it can't search backwards from the end.

In the actual case I came across just now that's no real problem. I can use
^Replace H and follow up with a new ^!Select PARAGRAPH. But in the general
case restoring the selection is not as simple as that and doing several
things to one selection is quite common for me.

Is there any way to transfer
^!Replace ", " >> " \& " HBSTI
to working on a variable?

(In case you wonder, this is about the last comma in a list of authors. And
yes, I know that TI are both superfluous here, but they are my base
standard and typed by default.)

Danke
Axel


--
/¯\ No | Dipl.-Ing. F. Axel Berger Tel: +49/ 221/ 7771 8067
\ / HTML | Roald-Amundsen-Straße 2a Fax: +49/ 221/ 7771 8069
 X in | D-50829 Köln-Ossendorf http://berger-odenthal.de
/ \ Mail | -- No unannounced, large, binary attachments, please! --


loro
 

Axel Berger wrote:
Unless I'm very wrong and miss the obvious, ^$StrReplace()$ has a
serious
limitation compared to ^!Replace, it can't search backwards from the
end.

In the actual case I came across just now that's no real problem. I can
use
^Replace H and follow up with a new ^!Select PARAGRAPH. But in the
general
case restoring the selection is not as simple as that and doing several
things to one selection is quite common for me.

Is there any way to transfer
^!Replace ", " >> " \& " HBSTI
to working on a variable?

(In case you wonder, this is about the last comma in a list of authors.
And
yes, I know that TI are both superfluous here, but they are my base
standard and typed by default.)


Axel, I'm very rusty and I'm not up do date with new additions to the clip language. Probably there are better ways, but here's how I used to do it, step by step. Find the index number of the comma, delete the comma, inject the & in its place.

^!Set %axel%=Goethe,Mann,Hesse
^!Set %pos%=^$StrPosRight(",";"^%axel%";false)$
^!Set %axel%=^$StrDelete("^%axel%";^%pos%;1)$
^!Set %axel%=^$StrInsert("&";"^%axel%";^%pos%)$

Lotta

--
Computer says no.


Axel Berger
 

loro wrote:
Find the index number of the comma, delete the comma, inject the & in its place.
Thank you Lotta. Yes. I have to admit I was more or less aware of that
possibility but baulked at the complication. Seeing your solution in the
flesh, it is not actually as bad as having to build it felt. I always find
it hard to get my head round: do I use pos as it is, do I decrease it by
one, or do I increase it by one.


--
/¯\ No | Dipl.-Ing. F. Axel Berger Tel: +49/ 221/ 7771 8067
\ / HTML | Roald-Amundsen-Straße 2a Fax: +49/ 221/ 7771 8069
 X in | D-50829 Köln-Ossendorf http://berger-odenthal.de
/ \ Mail | -- No unannounced, large, binary attachments, please! --


flo.gehrke@...
 

On Wed, Jul 29, 2020 at 03:33 PM, Axel Berger wrote:
Is there any way to transfer ^!Replace ", " >> " \& " HBSTI to working on a variable?
this is about the last comma in a list of authors.
The last comma in a list followed by a space is matched with ',\s\Z'.

Try...

^!Set %Authors%="XXX^PYYY^PZZZ, "
^!Info ^%Authors%
^!Set %Authors%=^$StrReplace(",\s\Z";"";"^%Authors%";R)$
^!Info ^%Authors%

This is using the new ^$StrReplace$ that was added in NT 7.0.

Regards,
Flo


Axel Berger
 

flo.gehrke@... wrote:
The last comma in a list followed by a space is matched with ',\s\Z'.
Thanks. I didn't know \z and just looked it up and tried ti understand. If
I get it right, your suggesetion would deal with trailing commas. That's
not what I mean.

I have a list like "Peter, Paul, Mary" and want "Peter, Paul & Mary" Of
course Mary could be a name of several parts not all of which are letters.
The only shure thing is, the 'name' must not contain ", ".

It's easy with
^!Replace ", " >> " & " HB

Danke
Axel


--
/¯\ No | Dipl.-Ing. F. Axel Berger Tel: +49/ 221/ 7771 8067
\ / HTML | Roald-Amundsen-Straße 2a Fax: +49/ 221/ 7771 8069
 X in | D-50829 Köln-Ossendorf http://berger-odenthal.de
/ \ Mail | -- No unannounced, large, binary attachments, please! --


flo.gehrke@...
 

I'm referring to your question...

Is there any way to transfer ^!Replace ", " >> " \& " HBSTI to working on a
variable? how to change the contents of a variable.
However, your proposal only works in a document.

Regarding your original question, a solution might be...

^!Set %Variable%=^$StrReplace(",\s(?!.*,\s";"";^%Variable%;R)$

See https://regex101.com/r/32y4JQ/1

Regards,
Flo