UE Regular Expressions Problem

Help with writing and running scripts

UE Regular Expressions Problem

Postby Skwerm » Fri Apr 13, 2007 2:17 pm

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?
User avatar
Skwerm
Basic User
Basic User
 
Posts: 14
Joined: Mon Aug 08, 2005 11:00 pm

Re: UE Regular Expressions Problem (Update)

Postby Skwerm » Fri Apr 13, 2007 2:48 pm

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?
User avatar
Skwerm
Basic User
Basic User
 
Posts: 14
Joined: Mon Aug 08, 2005 11:00 pm

Re: UE Regular Expressions Problem

Postby Bego » Fri Apr 13, 2007 5:43 pm

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
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: UE Regular Expressions Problem

Postby Skwerm » Fri Apr 13, 2007 6:01 pm

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.
User avatar
Skwerm
Basic User
Basic User
 
Posts: 14
Joined: Mon Aug 08, 2005 11:00 pm

Re: UE Regular Expressions Problem

Postby mrainey56 » Fri Apr 13, 2007 7:37 pm

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.
User avatar
mrainey56
Master
Master
 
Posts: 212
Joined: Tue Jul 27, 2004 11:00 pm
Location: Spartanburg, South Carolina

Re: UE Regular Expressions Problem

Postby Mofi » Tue May 08, 2007 7:53 am

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.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: UE Regular Expressions Problem

Postby SamHasler » Tue May 08, 2007 11:20 am

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.
User avatar
SamHasler
Basic User
Basic User
 
Posts: 16
Joined: Mon Aug 16, 2004 11:00 pm
Location: UK

Re: UE Regular Expressions Problem

Postby Mofi » Tue May 08, 2007 2:25 pm

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.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Scripts