Replace a string at the beginning of every line

Replace a string at the beginning of every line

2
NewbieNewbie
2

    Sep 14, 2023#1

    Hi all,

    I want to replace all 0000 at the beginning of every line in the actual file with two letters AB with a script.

    Code: Select all

    00000012345678 . . .
    00000001234567 . . .
    00000000123456 . . .
    00000012345678 . . .
    My code:

    Code: Select all

    UltraEdit.activeDocument.top();
    UltraEdit.activeDocument.findReplace.replaceAll=true;
    UltraEdit.activeDocument.findReplace.matchCase=true;
    UltraEdit.activeDocument.findReplace.replace("0000", "AB");
    Result:

    Code: Select all

    AB0012345678 . . .
    AB0001234567 . . .
    ABAB123456 . . .
    AB0012345678 . . ..
    My first try with a script is not really good.
    Line 3 should be:

    Code: Select all

    AB0000123456 . . .
    How to solve this problem?

    6,612548
    Grand MasterGrand Master
    6,612548

      Sep 14, 2023#2

      There must be used a regular expression replace with the anchor % (UltraEdit) or ^ (Unix/Perl) for matching only 0000 at beginning of a line.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         // UltraEdit.columnModeOff() is for UltraEdit for Window while
         // UltraEdit.activeDocument.columnModeOff() is for UltraEdit for Linux/Mac.
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
         // Move caret to top of the active file.
         UltraEdit.activeDocument.top();
      
         // Define all parameters for an UltraEdit regular expression replace
         // all in entire active file searching for the string 0000 at beginning
         // of a line and replace each found occurrence with the string AB.
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=true;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         // The IF condition is for UltraEdit version 13.xx which does not have
         // the search in column feature. Just the line inside the IF condition
         // is needed on newer versions of UltraEdit.
         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;
         UltraEdit.ueReOn();     // UltraEdit regular expression engine
         UltraEdit.activeDocument.findReplace.replace("%0000", "AB");
      }
      
      The last two lines above the last line with just } could be also:

      Code: Select all

         UltraEdit.unixReOn();   // Unix regular expression engine
         UltraEdit.activeDocument.findReplace.replace("^0000", "AB");
      or

      Code: Select all

         UltraEdit.perlReOn();   // Perl regular expression engine
         UltraEdit.activeDocument.findReplace.replace("^0000", "AB");
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        Sep 15, 2023#3

        Mofi, your code solution works for me.
        Thanks!