Forum Stats

  • 3,728,127 Users
  • 2,245,554 Discussions
  • 7,853,345 Comments

Discussions

Policy modelling rule to do a string replace

Hello,

I've looked at the text-related rules for handling strings:-


However, I can see no way to do a "string replace", e.g. to replace all spaces with an X:- something like REPLACE (<text>," ","X").


Am I missing something - or do we need to revert to JavaScript to do this?

Best Answer

  • Richard Napier
    Richard Napier Member Posts: 180 Silver Badge
    Accepted Answer

    Hi

    The cheapest and simplest way to do it would probably be to use the onSubmit event extension. Assuming you have an attribute whose name is myAttribute, and that it is either on the Screen or available via the JSON data extension file, then you could do this (off the top of my head).

    OraclePolicyAutomation.AddExtension({

      onSubmit: function(evt) {

    evt.interview.getExtensionData();

    // Get Old and New values for replacement

           var myUnwantedString = evt.interview.getValue("unwanted");

    var myWantedString = evt.interview.getValue("wanted");

    // Create a RegEx object based on the above, adjust as appropriate to make global, case insensitive etc.

    var myRegex = new RegExp(myUnwantedString, "g");

    // Perform the replace

    var myResult = evt.interview.getValue("myAttribute").replace(myRegex, myWantedString)

    // Update the value of the attribute

            evt.interview.setInputValue("myAttribute", myResult)

        }

      });

    This will perform a global RegEx replace on the content of the attribute and update the value before the user is sent to the next Screen.

    Hope this helps!

    I can send you a copy of the project.

    Richard

Answers

  • Richard Napier
    Richard Napier Member Posts: 180 Silver Badge

    Hi Jonathan

    One way I have approached something similar in the past - and I appreciate it may not correspond to your scenario - is to break the string into its component characters. So I would proceed thus:

    iteratively infer the characters as instances of an entity. So in the example below, the input value represents your starting string:

    It is much easier then to inspect each single character and decide what to do with it. In my case, if it is "X" then it should be switched to "Y" and so on. You can apply whatever validation you need, and provide the new value as appropriate.

    Then just roll the whole thing back up again using the character number as the basis for a loop, to create the output string.

    As I said above this may not need your needs but it does allow to handle any variable length string and perform validation / changes on each element before returning it.

    Feel free to respond if I am not understanding the scenario. And yes, of course, if this is an Interview then an Input extension could provide you with something similar using RegEx matches to find / replace the offending characters. Happy to share an example of that if you want one.

    Hope that helps,

    Richard

  • Jonathan Taylor
    Jonathan Taylor Member Posts: 13 Blue Ribbon

    Hello,


    Thanks for that. How would it work if you wanted replace in general. E.g. Change "Hello" for "Goodbye".

    I presume this would require falling back to an input extension. If you would be kind enough - could you please share an example?

    Are there any plans to add this functionality - since it is such a common function?

    Thanks.

  • Richard Napier
    Richard Napier Member Posts: 180 Silver Badge

    Hello Again

    Just to be sure I'm getting the context. This is a text attribute that the user enters, let's say with a value of "Hello". And you want it to change to "Goodbye".

    Or is this a text attribute that contains a lot of text, in which there may be "Hello" and you want to replace all instances of "Hello" with "Goodbye".

    Just trying to grasp the exact scenario. Thanks for your help!

    R

  • Jonathan Taylor
    Jonathan Taylor Member Posts: 13 Blue Ribbon

    Hello,

    All instances, just like a general-purpose REPLACE you find in languages such as Java/JavaScript/SQL etc.

    Thanks

  • Richard Napier
    Richard Napier Member Posts: 180 Silver Badge

    Hi

    Well, there is no way for us to create essentially custom functions that would work in all circumstances. A JavaScript extension gives us the ability to create a JS Function and use it on any controls that need to have the replacement done, but of course they will not work in Web Service-based projects or digital assistant integrations. Rulescript would have offered an alternative for all scenarios, but Rulescript is no longer supported or documented / approved.

    So if the JavaScript extension is the assumed route, then this would be for interviews only. I'll knock something up and post it here.

    Thanks for the clarification

    Richard

  • Richard Napier
    Richard Napier Member Posts: 180 Silver Badge
    Accepted Answer

    Hi

    The cheapest and simplest way to do it would probably be to use the onSubmit event extension. Assuming you have an attribute whose name is myAttribute, and that it is either on the Screen or available via the JSON data extension file, then you could do this (off the top of my head).

    OraclePolicyAutomation.AddExtension({

      onSubmit: function(evt) {

    evt.interview.getExtensionData();

    // Get Old and New values for replacement

           var myUnwantedString = evt.interview.getValue("unwanted");

    var myWantedString = evt.interview.getValue("wanted");

    // Create a RegEx object based on the above, adjust as appropriate to make global, case insensitive etc.

    var myRegex = new RegExp(myUnwantedString, "g");

    // Perform the replace

    var myResult = evt.interview.getValue("myAttribute").replace(myRegex, myWantedString)

    // Update the value of the attribute

            evt.interview.setInputValue("myAttribute", myResult)

        }

      });

    This will perform a global RegEx replace on the content of the attribute and update the value before the user is sent to the next Screen.

    Hope this helps!

    I can send you a copy of the project.

    Richard

  • Jonathan Taylor
    Jonathan Taylor Member Posts: 13 Blue Ribbon

    Thanks for that - it has put me on the track.

  • Ian G Clough
    Ian G Clough Member Posts: 19 Green Ribbon

    Hi Jonathan,

    Perhaps you could provide a bit more overall context of what you are trying to achieve, is this an interactive interview or stateless policy evaluation? Intelligent Advisor is an excellent tool but it isn't a general purpose programming environment and there may be alternative ways to approach your problem.

    Jasmine Lee-Oracle
  • Jonathan Taylor
    Jonathan Taylor Member Posts: 13 Blue Ribbon

    The use case is pretty much covered in the video from Richard's answer.


    In my particular case, I wanted to remove spaces from a reference number in an interview field so that it is in a "normalised" form, I can then use that for e.g. validating or showing in a form document. E.g. a credit card number may be entered with or without spaces.


    But, thinking ahead I've had many past experiences where REPLACE() was used generally in a form, such as removing the "international" part of a phone number (+44) or removing spaces from a UK postcode.


    The docs show several string-handling functions such as SubString - a replace() seems like an omission to me.

Sign In or Register to comment.