Tapatalk

UE Regular Expressions Problem

UE Regular Expressions Problem

14
Basic UserBasic User
14

    Apr 13, 2007#1

    I'm trying to set a variable to a regular expression and then use that variable in a findReplace. I just can't get it to work, even with a simple expression. The following script should find the word "software" but it just finds the trailing 'e'.

    Code: Select all

    // Set UE Application Objects
    UltraEdit.ueReOn;  // Use UltraEdit Regular Expressions
    
    // Set search variables
    //var rexCodeTypes = "% 20[1-2] ";  <-- This is what I'm trying to do
    var rexCodeTypes = "s*e";  //  <-- This is the simple test that doesn't work
    
    
    // Retrieve and store active document's index
    var activeDocIndex = getActiveDocumentIndex();
    
    // Return focus to original document
    UltraEdit.document[activeDocIndex].setActive();
    
    
    UltraEdit.document[activeDocIndex].top();
    UltraEdit.document[activeDocIndex].findReplace.regExp = true;
    UltraEdit.document[activeDocIndex].findReplace.find(rexCodeTypes);
    
    // Functions -----------------------------------------------------------------
    
    /* Find the tab index of the active document */
    function getActiveDocumentIndex() {
       var tabindex = -1; /* start value */
    
       for (i = 0; i < UltraEdit.document.length; i++)
       {
          if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
             tabindex = i;
             break;
          }
       }
       return tabindex;
    }

    Can someone help?

      Re: UE Regular Expressions Problem (Update)

      Apr 13, 2007#2

      Well, now things are worse...

      I checked for UE32 updates and found that I had missed the 13.0a that was released yesterday. So I was only one update behind, but thought doing the update might fix the scripting error. Man, was I wrong!

      Now not only does my script not work, it crashes UE and creates a dump file.

      Can anyone see anything in my script that should cause a full-on UE explosion?

      344
      MasterMaster
      344

        Apr 13, 2007#3

        Hi Skwerm,

        I can confirm crash in 13.00a, so best: Post them the dump.

        I got your example to work with hardcoded document[0], so maybe the undocumented .path function is the problem:

        Code: Select all

        var rexCodeTypes = "s.*e"; 
        UltraEdit.document[0].top();
        UltraEdit.document[0].findReplace.regExp = true;
        UltraEdit.document[0].findReplace.find(rexCodeTypes);
        
        In your example it only finds an e because you forgot a . before the *
        See my var above.

        rds Bego
        Normally using all newest english version incl. each hotfix. Win 10 64 bit

        14
        Basic UserBasic User
        14

          Apr 13, 2007#4

          Bego wrote:

          Code: Select all

          var rexCodeTypes = "s.*e"; 
          UltraEdit.document[0].top();
          UltraEdit.document[0].findReplace.regExp = true;
          UltraEdit.document[0].findReplace.find(rexCodeTypes);
          
          In your example it only finds an e because you forgot a . before the *
          See my var above.

          rds Bego
          I'm using UE's Legacy RE's, not Unix. My original script had:

          Code: Select all

          UltraEdit.ueReOn;  // Use UltraEdit Regular Expressions
          Did you change the script to Unix RE's when you got it to work? I suppose I'll try switching over to Unix style RE's and see if I have success there.

          The crash, by the way, seems to be related to the setActive() command. I've posted that to another thread.

          Thanks.

          206
          MasterMaster
          206

            Apr 14, 2007#5

            There used to be UE style and Unix style, and you could select the style from within the old UE macro language. Changing the regex style inside a macro would also change the regex configuration selection (an aggravation).

            The Perl style was a fairly recent edition to the mix.
            Software For Metalworking
            http://closetolerancesoftware.com

            6,685587
            Grand MasterGrand Master
            6,685587

              May 08, 2007#6

              setActive lets UE crash here, that's true.

              Problem with the regular expression engine is caused by wrong command

              UltraEdit.ueReOn; // Use UltraEdit Regular Expressions

              Correct would be

              UltraEdit.ueReOn(); // Use UltraEdit Regular Expressions

              Then the UltraEdit engine will be really selected and s*e will work. It looks like wrong UltraEdit.??? with missing () is simply always ignored without an error message. I'm not a JavaScript expert and so I don't know, if this is correct or not.

              And it looks like the script environment use by default always the Perl or the Unix engine. I have not really tested which one. But surely the currently specified engine in the configuration dialog is not used as default. So like in macros always specify at top of the script CORRECT the regular expression engine when using regular expression find/replaces.

              I suggest to use the tags in tag group "UE/UES Script Commands" with the tag list view to avoid such errors like missing () with big influence on the script execution.
              Best regards from an UC/UE/UES for Windows user from Austria

              16
              Basic UserBasic User
              16

                May 08, 2007#7

                Mofi wrote:Problem with the regular expression engine is caused by wrong command

                UltraEdit.ueReOn; // Use UltraEdit Regular Expressions

                Correct would be

                UltraEdit.ueReOn(); // Use UltraEdit Regular Expressions

                Then the UltraEdit engine will be really selected and s*e will work. It looks like wrong UltraEdit.??? with missing () is simply always ignored without an error message. I'm not a JavaScript expert and so I don't know, if this is correct or not.
                If it works anything like how JavaScript works in browsers then without the () it might be evaluating to true or false based on whether or not the function exists. I assume a true or false statement on it's own on a line is then silently ignored.

                So you could do something like:

                Code: Select all

                if(UltraEdit.ueReOn) {
                //Do something using this function if it's available
                }
                I don't have a version of UE/UES with scripting so I'd be interested to know if that works. It could prove useful for writing scripts in future when there are newer versions of the scripting engine supporting extra functions and you want to write a script that will work in any engine, but will take advantage of newer functions for speed if they are available.

                6,685587
                Grand MasterGrand Master
                6,685587

                  May 08, 2007#8

                  SamHasler, thanks. You are absolutely right and it is working as you described. I used following test code

                  Code: Select all

                  if(UltraEdit.ueReOn) UltraEdit.activeDocument.write("Function UltraEdit.ueReOn exists!\r\n");
                  else UltraEdit.activeDocument.write("Function UltraEdit.ueReOn does not exist!\r\n");
                  
                  if(UltraEdit.ueReOff) UltraEdit.activeDocument.write("Function UltraEdit.ueReOff exists!\r\n");
                  else UltraEdit.activeDocument.write("Function UltraEdit.ueReOff does not exist!\r\n");
                  
                  When I execute the script, I get the correct result:

                  Code: Select all

                  Function UltraEdit.ueReOn exists!
                  Function UltraEdit.ueReOff does not exist!
                  And it can be really used to evaluate if a function exists before using it. I have tested that too.
                  Best regards from an UC/UE/UES for Windows user from Austria