Script find/replace/add values in text

Script find/replace/add values in text

3

    Feb 11, 2023#1

    Hi everybody!

    Someone can help me? I want to do a find/replace in my text with UltraEdit-32 v14.00.
    I already tried but I’m always stuck.

    Here is my text:

    The Symbolism of Flower Colors is Steeped in Tradition : red, white, blue, etc.
    <level value_level="001">Blue</level>
    <p>A blue flower stands for desire, love, and inspiration. It is a flower that represents the metaphysical striving for the impossible and infinite.</p>
    <level value_level="002">Red</level>
    <p>Although red flowers are most commonly associated with feelings of true love and passion, they can also be used to convey respect, desire, and courage.</p>
    <level value_level="003">White</level>
    <p>White flowers can mean reverence and humility, purity and innocence or sympathy for a bereavement.</p>


    And here is my expected result:

    The Symbolism of Flower Colors is Steeped in Tradition : <renv id="002">red</renv>, <renv id="003">white</renv>, <renv id="001">blue</renv>, etc.
    <level value_level="001">Blue</level>
    <p>A blue flower stands for desire, love, and inspiration. It is a flower that represents the metaphysical striving for the impossible and infinite.</p>
    <level value_level="002">Red</level>
    <p>Although red flowers are most commonly associated with feelings of true love and passion, they can also be used to convey respect, desire, and courage.</p>
    <level value_level="003">White</level>
    <p>White flowers can mean reverence and humility, purity and innocence or sympathy for a bereavement.</p>


    The goal is to find the keyword and add the corresponding values near the keyword (using a script).
    Thanks in advance ^_ ^

    6,687586
    Grand MasterGrand Master
    6,687586

      Feb 12, 2023#2

      It is unclear for me which occurrence of the words red, white and blue should be enclosed in the renv tags and which should be kept as is like the words in the paragraphs below the level elements. There is not defined a rule in task description which should be used for level string matching. The script below replaces for that reason the first occurrence of a level string by the matched string surrounded with the appropriate renv tags. The script produces for the posted input the expected output. The script was tested with UltraEdit for Windows v2022.2.0.48 and also v14.00b+1.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
         // Select the entire content of active file.
         UltraEdit.activeDocument.selectAll();
         if (UltraEdit.activeDocument.isSel())
         {
            // Find all occurrences of <level value_level="\d+">[^<]+</level> in entire file and
            // create an array of strings matched by the Perl regular expression: value_level="\d+">[^<]+
            var asLevels = UltraEdit.activeDocument.selection.match(/value_level="\d+">[^<]+(?=<\/level>)/g);
            if (asLevels !== null)  // Was any matching string found?
            {
               // Define the parameters for a case-insensitive Perl regular expression replace
               // in active file to replace the first occurrence of a level string without or
               // with start and close tag of element renv around by appropriate renv element.
               UltraEdit.perlReOn();
               UltraEdit.activeDocument.findReplace.mode=0;
               UltraEdit.activeDocument.findReplace.matchCase=false;
               UltraEdit.activeDocument.findReplace.matchWord=false;
               UltraEdit.activeDocument.findReplace.regExp=true;
               UltraEdit.activeDocument.findReplace.searchDown=true;
               if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
               {
                  UltraEdit.activeDocument.findReplace.searchInColumn=false;
               }
               UltraEdit.activeDocument.findReplace.preserveCase=false;
               UltraEdit.activeDocument.findReplace.replaceAll=false;
               UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
      
               // Define the variables used in the loop below.
               var nTagEndPosition;
               var sLevelNumber;
               var sLevelString;
      
               // Process all level element values in a loop.
               for (var nLevelIndex = 0; nLevelIndex < asLevels.length; nLevelIndex++)
               {
                  // Get the character index position of the closing angle bracket.
                  nTagEndPosition = asLevels[nLevelIndex].indexOf(">");
                  // The value level is the substring from character position 13 to the
                  // character position one character before the closing angle bracket.
                  sLevelNumber = asLevels[nLevelIndex].substring(13,nTagEndPosition-1);
                  // The value string is the substring from position of the next
                  // character after the closing angle bracket to end of the string.
                  sLevelString = asLevels[nLevelIndex].substring(nTagEndPosition+1);
                  // Move the caret in active file to top.
                  UltraEdit.activeDocument.top();
                  // Run a single case-insensitive Perl regular expression replace to replace the
                  // first occurrence of the level string without or with surrounding renv tags by
                  // the found level string with inserting/replacing the appropriate renv element tags.
                  UltraEdit.activeDocument.findReplace.replace('(?:<renv id="\\d+">)?(\\b'+sLevelString+'\\b)(?!</level>)(?:</renv>)?',
                                                               '<renv id="'+sLevelNumber+'">\\1</renv>');
               }
               // The caret remains at position of last successful done replace.
            }
         }
      }
      
      Best regards from an UC/UE/UES for Windows user from Austria

      3

        Feb 13, 2023#3

        Hi Mofi,
        Thanks for answering ^_^
        Yeah, I had to explain the principle. I rephrase my example  :

        The Symbolism of Flower Colors is Steeped in Tradition : red, white, blue, etc.....

        Learn more about blue
        Learn more about white
        Learn more about red

        ..............
        <level value_level="01">Blue</level>
        <p>A blue flower stands for desire, love, ....</p>
        <level value_level="02">Red</level>
        <p>Although red flowers are most commonly associated with feelings of true love and passion, ....</p>
        <level value_level="03">White</level>
        <p>White flowers can mean reverence and humility, purity and ....</p>

        The objective is to insert a renv tag automatically (the key word after "Learn more about") with their appropriate id. 
        The id is obtained from <level value_level="...">...</level>.
        The expected result:


        The Symbolism of Flower Colors is Steeped in Tradition : red, white, blue, etc.....

        Learn more about <renv id="01">blue</renv>
        Learn more about <renv id="03">white</renv>
        Learn more about <renv id="02">red</renv>

        ..............
        <level value_level="01">Blue</level>
        <p>A blue flower stands for desire, love, ....</p>
        <level value_level="02">Red</level>
        <p>Although red flowers are most commonly associated with feelings of true love and passion, ....</p>
        <level value_level="03">White</level>
        <p>White flowers can mean reverence and humility, purity and ....</p>

        Thanks in advance ^_ ^

        6,687586
        Grand MasterGrand Master
        6,687586

          Feb 13, 2023#4

          There can be used either the script as posted first by me for the given example in your second post or there are changed in the script the lines 31 (32 in script below) and 55 (57 in script below) as it can be seen below on which the two lines and two comments are adapted for a replace all after Learn more about. The last comment is deleted additionally, too.

          Code: Select all

          if (UltraEdit.document.length > 0)  // Is any file opened?
          {
             // Define environment for this script.
             UltraEdit.insertMode();
             if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
             else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
          
             // Select the entire content of active file.
             UltraEdit.activeDocument.selectAll();
             if (UltraEdit.activeDocument.isSel())
             {
                // Find all occurrences of <level value_level="\d+">[^<]+</level> in entire file and
                // create an array of strings matched by the Perl regular expression: value_level="\d+">[^<]+
                var asLevels = UltraEdit.activeDocument.selection.match(/value_level="\d+">[^<]+(?=<\/level>)/g);
                if (asLevels !== null)  // Was any matching string found?
                {
                   // Define the parameters for a case-insensitive Perl regular expression
                   // replace all in active file to replace all occurrences of a level
                   // string after the string "Learn more about " without or with start
                   // and close tag of element renv around by appropriate renv element.
                   UltraEdit.perlReOn();
                   UltraEdit.activeDocument.findReplace.mode=0;
                   UltraEdit.activeDocument.findReplace.matchCase=false;
                   UltraEdit.activeDocument.findReplace.matchWord=false;
                   UltraEdit.activeDocument.findReplace.regExp=true;
                   UltraEdit.activeDocument.findReplace.searchDown=true;
                   if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
                   {
                      UltraEdit.activeDocument.findReplace.searchInColumn=false;
                   }
                   UltraEdit.activeDocument.findReplace.preserveCase=false;
                   UltraEdit.activeDocument.findReplace.replaceAll=true;
                   UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
          
                   // Define the variables used in the loop below.
                   var nTagEndPosition;
                   var sLevelNumber;
                   var sLevelString;
          
                   // Process all level element values in a loop.
                   for (var nLevelIndex = 0; nLevelIndex < asLevels.length; nLevelIndex++)
                   {
                      // Get the character index position of the closing angle bracket.
                      nTagEndPosition = asLevels[nLevelIndex].indexOf(">");
                      // The value level is the substring from character position 13 to the
                      // character position one character before the closing angle bracket.
                      sLevelNumber = asLevels[nLevelIndex].substring(13,nTagEndPosition-1);
                      // The value string is the substring from position of the next
                      // character after the closing angle bracket to end of the string.
                      sLevelString = asLevels[nLevelIndex].substring(nTagEndPosition+1);
                      // Move the caret in active file to top.
                      UltraEdit.activeDocument.top();
                      // Run a case-insensitive Perl regular expression replace all to replace all
                      // occurrences of the level string after "Learn more about " without or with
                      // surrounding renv tags by the found level string with inserting/replacing
                      // the appropriate renv element tags.
                      UltraEdit.activeDocument.findReplace.replace('(?<=Learn more about )(?:<renv id="\\d+">)?(\\b'+sLevelString+'\\b)(?!</level>)(?:</renv>)?',
                                                                   '<renv id="'+sLevelNumber+'">\\1</renv>');
                   }
                }
             }
          }
          
          Best regards from an UC/UE/UES for Windows user from Austria

          3

            Feb 17, 2023#5

            Thanks for your help 😉