Perl transliteration operator to replace occurrences of characters in SEARCHLIST with characters in REPLACEMENTLIST

Perl transliteration operator to replace occurrences of characters in SEARCHLIST with characters in REPLACEMENTLIST

2
NewbieNewbie
2

    Feb 18, 2020#1

    I have/use UltraEdit Text/Hex Editor (x64) Version 26.00.0.48

    Does any version of UltraEdit support the Perl transliteration operator to replace occurrences of characters in SEARCHLIST with characters in REPLACEMENTLIST? I am referring to the operator described in this URL: Perl Tutorial: Substitution and Translation

    Perl example: $str =~ tr/ac/bd/;

    If UltraEdit does not support that operator, is there a way to achieve the same effect with a script that will replace characters in the first string with the corresponding character in the second list?
    Mark S. Harcourt | XML IT Specialist | Government Publishing Office

    6,678579
    Grand MasterGrand Master
    6,678579

      Feb 19, 2020#2

      I don't know if the Boost regular expression library used by UltraEdit for regular expression finds/replaces in Perl syntax supports transliteration.

      There are in my opinion not many use cases on which transliteration makes sense. However, it is simple to use an UltraEdit script for transliteration.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Prompt the user for the search and replace characters.
         var sSearch;
         var sReplace;
         do
         {
             sSearch = UltraEdit.getString("Enter the characters to search for:",1);
         }
         while (!sSearch.length);
         do
         {
             sReplace = UltraEdit.getString("Enter the characters to replace with:",1);
         }
         while (!sReplace.length);
      
         // Prompt the user if the replaces should be done even with number of
         // characters to search for not equal number of characters to replace with.
         var nDoReplaces = 1;
         if (sSearch.length != sReplace.length)
         {
            var sPrompt = "There ";
            if (sSearch.length == 1)
            {
               sPrompt += "is " + sSearch.length + " character";
            }
            else
            {
               sPrompt += "are " + sSearch.length + " characters";
            }
            sPrompt += " to seach for, but\nthere ";
            if (sReplace.length == 1)
            {
               sPrompt += "is " + sReplace.length + " character";
            }
            else
            {
               sPrompt += "are " + sReplace.length + " characters";
            }
            sPrompt += " to replace with.\nContinue anyway (0=no / 1=yes)?";
            nDoReplaces = UltraEdit.getValue(sPrompt,1);
         }
      
         // Should the replaces be done?
         if (nDoReplaces)
         {
            // Define environment for this script.
            UltraEdit.insertMode();
            UltraEdit.columnModeOff();
      
            // Define the parameters for the case-sensitive, non-regular
            // expression replaces executed from current position in active
            // file to end of file or on just selected text if there is a
            // selection in active file.
            UltraEdit.ueReOn();
            if (!UltraEdit.activeDocument.isSel())
            {
               UltraEdit.activeDocument.findReplace.mode=0;
               UltraEdit.activeDocument.findReplace.selectText=false;
            }
            else
            {
               UltraEdit.activeDocument.findReplace.mode=1;
               UltraEdit.activeDocument.findReplace.selectText=true;
            }
            UltraEdit.activeDocument.findReplace.matchCase=true;
            UltraEdit.activeDocument.findReplace.matchWord=false;
            UltraEdit.activeDocument.findReplace.regExp=false;
            UltraEdit.activeDocument.findReplace.searchDown=true;
            UltraEdit.activeDocument.findReplace.searchInColumn=false;
            UltraEdit.activeDocument.findReplace.preserveCase=false;
            UltraEdit.activeDocument.findReplace.replaceAll=true;
            UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
      
            // Run the replaces for each character in search and replace string.
            do
            {
               // Get next character to search for and then remove
               // it from the list of characters to search for.
               var sSearchChar = sSearch.substr(0,1);
               sSearch = sSearch.substr(1);
               // Get next character to replace with and then remove
               // it from the list of characters to replace with.
               var sReplaceChar = sReplace.substr(0,1);
               sReplace = sReplace.substr(1);
               // Escape the character caret which is by default the
               // escape character in non-regular expression replaces.
               if (sSearchChar == "^") sSearchChar = "^^";
               if (sReplaceChar == "^") sReplaceChar = "^^";
               // Run the replace for this character pair.
               UltraEdit.activeDocument.findReplace.replace(sSearchChar,sReplaceChar);
            }
            while (sSearch.length && sReplace.length);
         }
      }
      
      The transliteration replaces are executed either on just selected text in case of there is a selection on starting the script or on file content from active position in file to end of file. The caret must be moved to top of the file with Ctrl+Home to do the transliteration on entire file content.

      The script is written to do the replaces directly on file content. So there is no file size limitation. Each replace produces one undo step if the file is loaded with usage of a temporary file.

      It would be also possible to load the selected block or everything from current position in file to end of file into memory of JavaScript core engine into a string variable (length is limited), do the character replaces on this string in memory and write the updated string (=block) back to file replacing the original (selected) text. That would produce no (on nothing changed) or just one undo record. Let me know if you would like a script using this alternate method to do the transliteration which would be also faster because of less window updates.
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        Feb 19, 2020#3

        Thank you, Mofi. Your reply is very useful and perfectly adequate. I agree that the need for the transliteration operator in UltraEdit is vanishingly small. (I've been using UltraEdit and its various regex engines for more than two decades, This is the first case/time I've wished for a transliteration operator or similar in UltraEdit.) In my own case, I have a set of non-XML characters in a non-XML file that I want/need to replace with valid XML characters to make it easier to transform using XSLT. So the input string is a set of no more than 6 to 8 characters.
        Mark S. Harcourt | XML IT Specialist | Government Publishing Office

        19576
        MasterMaster
        19576

          Feb 19, 2020#4

          Hi,

          you also can use this 2-step multireplace collection-of-often-needed-finds-and-re ... tml#p48753
          For single letter replace just remove the trailing/leading \b from the first pattern. The original purpose is to replace whole words.

          BR, Fleggy