No internet connection
  1. Home
  2. Questions

Using regex REPLACE in ThinkAutomation

By isedwinr @isedwinr
    2022-11-03 10:56:04.438Z

    Hello all,

    I'm trying to use Regex (in this case) to convert phone numbers to the same format
    Matching works fine via "Text Operation-> Extract: Regex" but I would also like to use Regex REPLACE

    I want to use this to reformat/normalize phonenumbers like I can on regexr.com for example
    See screenshot, top window is the match, below is the replace

    Does anyone know how I can get this to work in TA?

    BTW I think I can also acomplish it with a PowerShell command like this (did not try it yet) I saw on another forum
    '0271112222' -Replace '(?:0)(2\d)(?:\s)(\d{3})(?:\s)(\d{3,4})', '+64 $1 $2 $3'
    But i'd rather use a "normal" TA option for this

    Any ideas?

    Edwin

    Solved in post #3, click to view
    • 3 replies
    1. D
      Daniel Horton @daniel.horton
        2022-11-04 12:43:38.954Z

        Hi Edwin,

        You could use the Script options in ThinkAutomation to call a Regex.Replace function available to the .Net library via C#. Here is a sample from Microsoft - https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.replace?view=net-7.0

        Here is the code I used to replicate their example:

        using System;
        using System.Text;
        using ThinkAutomationCoreClasses.Scripting;
        using System.Text.RegularExpressions;
        
        public class ThinkAutomationScript
        {
        	
        	
            public string execute(currentMessage message)
            {
                try
                {
                    // Action execution code
                    // Get extracted fields/variable values using message.GetValue("name")
                    // Set extracted fields/variable values using message.SetValue("name","value")
                   	string input = "This is   text with   far  too   much   " + 
                             "white space.";
                    string pattern = "\\s+";
                    string replacement = " ";
        	    string result = Regex.Replace(input,pattern,replacement);
        			
        	    return result;
        			
                }
                catch (Exception e)
                {
                    // Pass the error back to the automation log
                    message.AddErrorToLog(e.Message);
                    return "";
                }
            }
        }
        

        Here is the console output from the call:

        Here is my output in ThinkAutomation's logs:

        1. D
          In reply toisedwinr:
          Daniel Horton @daniel.horton
            2022-11-04 14:15:13.091Z

            Based upon this requirement we will be adding a Replace Regex function to our Text Operation Action. This will be available in our next release.

            Reply1 LikeSolution
            1. I
              In reply toisedwinr:
              isedwinr @isedwinr
                2022-11-04 16:35:11.779Z

                Wonderful! thx for both answers!

                regards,
                Happy customer ;)