Question Replace Exact Words

anandknr

Member
Hi All,

Is there a function available in progress which will replace only exact words but not portion of it. I believe there is not.

Code:
define variable pcString as character no-undo.
define variable pcErrorWord as character no-undo.

assign pcString = "customer is abbreviated as cust."
       pcErrorWord = "cust".

message replace(pcString,pcErrorWord,"usr").

Above code outputs as "usromer is abbreviated as usr".
I wanted it to be "customer is abbreviated as usr".

Can someone help me to acheive this?


 

GregTomkins

Active Member
Progress' lack of RE support is one of the more difficult things to defend, IMO, though maybe it's finally there in more recent versions?

I'd shell out to Perl, or similar, for something like this. Absent that, you're going to have to write some hacky special-case-ridden hope-you-get-them-all code.
 

TheMadDBA

Active Member
If you are trying to do this as part of your application and not a mass data fix... then look into the SUBSTITUTE function instead.

If you are trying to do a mass data fix... you would need to use multiple/nested replace statements with all of the variations of word breaks.. and a lot of testing/validation afterwards.

REPLACE(x," cust.","usr.")
REPLACE(x," cust "," usr ")

etc. etc. etc.
 

RealHeavyDude

Well-Known Member
Basically it could be very simple if you define space as the only word break.

You could then loop over the entries and check each entry whether it matches your word:
Code:
do wordCount = 1 to num-entries ( "<your-character-value>", " " ):
    if entry ( wordCount, <your-character-value>, " " ) = "<your-word>" then do:
         /* Your stuff */
    end.
end.

If you need to take the other characters into account that should be interpreted as a word break ( for example the full stop ) then it gets more complicated. If for some reason there is no blank after a word break in <your-character-value> then the logic will fail.

Heavy Regards, RealHeavyDude.
 

joey.jeremiah

ProgressTalk Moderator
Staff member
you might want to have a look at the str_beautify() function in slibstr.p, in the progress standard libraries project at the oehive.org.

the first thing the function does is break down the string into words.

Code:
{slib/slibstr.i}

message str_beautify( "    hello world     , (  10x    )     . GOOD BYE       . ", no ).

/* returns "Hello world, (10x). Good bye." */

hth
 
Last edited:

JoseKreif

Member
Hi All,

Is there a function available in progress which will replace only exact words but not portion of it. I believe there is not.

Code:
define variable pcString as character no-undo.
define variable pcErrorWord as character no-undo.

assign pcString = "customer is abbreviated as cust."
       pcErrorWord = "cust".

message replace(pcString,pcErrorWord,"usr").

Above code outputs as "usromer is abbreviated as usr".
I wanted it to be "customer is abbreviated as usr".

Can someone help me to acheive this?
NEW CODE NEW CODE NEW CODE NEW CODE NEW CODE

Okay, I actually enjoy programming, so I sat down and wrote a more compelte working procedure that should easily plug and play




---------------------------


Code:
def var sent as char no-undo.


sent = "Today I will go to Tony's Pizza-Ria to get some tomato pie. Plus, Its best to get top server for tomorrow's toga party. No lets go to the marker and get some Tostitos".

run replaceWords(input-output sent,"to", "FOOBAR").

message sent view-as alert-box.

procedure replaceWords:


  def input-output param vstr as char no-undo. /* string */
  def input param oword  as char no-undo. /* OLD WORD*/
  def input param nword  as char no-undo. /* NEW WORD */

  def var x   as int no-undo. /* COUNTER */

  do x = 1 to length(vstr):

    if substr(vstr, x, (length(oword) + 2)) = " " + oword + " " then
      vstr = replace(vstr, substr(vstr, x,length(oword) + 2), " " + nword + " ").

    else if substr(vstr, x, (length(oword) + 2)) = " " + oword + "." then
      vstr = replace(vstr, substr(vstr, x,length(oword) + 2), " " + nword + ".").

    else if substr(vstr, x, (length(oword) + 2)) = " " + oword + "!" then
      vstr = replace(vstr, substr(vstr, x,length(oword) + 2), " " + nword + "!").

    else if substr(vstr, x, (length(oword) + 2)) = " " + oword + ":" then
      vstr = replace(vstr, substr(vstr, x,length(oword) + 2), " " + nword + ":").

    else if substr(vstr, x, (length(oword) + 2)) = " " + oword + "?" then
      vstr = replace(vstr, substr(vstr, x,length(oword) + 2), " " + nword + "?").

    else if substr(vstr, x, (length(oword) + 2)) = " " + oword + "(" then
      vstr = replace(vstr, substr(vstr, x,length(oword) + 2), " " + nword + "(").

    else if substr(vstr, x, (length(oword) + 2)) = " " + oword + "/" then
      vstr = replace(vstr, substr(vstr, x,length(oword) + 2), " " + nword + "/").

    else if substr(vstr, x, (length(oword) + 2)) = " " + oword + ";" then
      vstr = replace(vstr, substr(vstr, x,length(oword) + 2), " " + nword + ";").

    else if substr(vstr, x, (length(oword) + 2)) = " " + oword + "+" then
      vstr = replace(vstr, substr(vstr, x,length(oword) + 2), " " + nword + "+").

    else if substr(vstr, x, (length(oword) + 2)) = " " + oword + "-" then
      vstr = replace(vstr, substr(vstr, x,length(oword) + 2), " " + nword + "-").

    else if substr(vstr, x, (length(oword) + 2)) = " " + oword + "*" then
      vstr = replace(vstr, substr(vstr, x,length(oword) + 2), " " + nword + "*").

    else if substr(vstr, x, (length(oword) + 2)) = " " + oword + "=" then
      vstr = replace(vstr, substr(vstr, x,length(oword) + 2), " " + nword + "=").

    end.

end.




--------------------------------

EDIT: 3/31/2016

I will now handle words that don't end with a space
 
Last edited:
Top