0

In Ultraedit using perl Regex, I am trying to replace a strings DATA0 with DATA8, DATA1 with DATA9, and so on. I know how to get the match to happen in Ultraedit's Find Dialog using DATA\d.

In order to capture the digit, I use DATA(\d), and in the "Replace With:" box I can access the group with $1, DATA$1+8 but this, obviously, results in text DATA0+8, which makes sense.

Is there an eval() that can be done in Ultraedit's replace dialog in order to modify the captured group variable $1?

I realize this can be done in the javascript integration with Ultraedit, but I would rather be able to do this out of the box from the Replace Dialog.

Okkenator
  • 1,654
  • 1
  • 15
  • 27
  • I don't see how Perl is related to this tool at all, but there are examples of find/replace in their documentation: http://www.ultraedit.com/support/tutorials_power_tips/ultraedit/regular_expressions.html – Hunter McMillen Dec 02 '15 at 15:18
  • @Hunter: It supports 3 regex styles (Ultraedit, Unix, and Perl) and I am specifically using the Perl regex style. The examples in your link are useful but specifically lack the ability to modify the captured groups, which is my question. I might simply not be possible currently. – Okkenator Dec 02 '15 at 15:28

2 Answers2

2

No, UltraEdit can't do that.

You could actually use Perl

perl -i.bak -pe"s/DATA\K(\d+)/$1+8/eg" "C:\..."       5.10+

perl -i.bak -pe"s/(DATA)(\d+)/$1.($2+8)/eg" "C:\..."
ikegami
  • 367,544
  • 15
  • 269
  • 518
1

Text editors like UltraEdit do not support evaluation of a formula during a replace operation. This requires a script and a script interpreter like Perl or JavaScript.

UltraEdit has built-in the JavaScript interpreter. This task can be therefore done also with UltraEdit using an UltraEdit script, for example the one below.

if (UltraEdit.document.length > 0)  // Is any file opened?
{
   // Define environment for this script.
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();

   // Move caret to top of the active file.
   UltraEdit.activeDocument.top();

   // Defined all Perl regular expression Find parameters.
   UltraEdit.perlReOn();
   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;
   if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
   {
      UltraEdit.activeDocument.findReplace.searchInColumn=false;
   }

   // Search for each number after case-sensitive word DATA using
   // a look-behind to get just the number selected by the find.
   // Each backslash in search string for Perl regular expression
   // engine of UltraEdit must be escaped with one more backslash as
   // the backslash is also the escape character in JavaScript strings.
   while(UltraEdit.activeDocument.findReplace.find("(?<=\\bDATA)\\d+"))
   {
      // Convert found and selected string to an integer using decimal
      // system, increment the number by eight, convert the incremented
      // number back to a string using again decimal system and write the
      // increased number string to file overwriting the selected number.
      var nNumber = parseInt(UltraEdit.activeDocument.selection,10) + 8;
      UltraEdit.activeDocument.write(nNumber.toString(10));
   }
}
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Thank you. This is great for the specific case I mentioned but the reason I would not want to use the script is because it does not work for the general case. This could be changed to prompt the user for the regex search and replace terms but I expect the challenge is making it not evaluate the replace term too early. – Okkenator Dec 04 '15 at 15:50
  • @Mofi, GREAT answer. Thank you so much for the complete example. I've been prototyping a complex html file conversion process using regexes in UE, figuring I'd move to a programming language once I got the transformation sequence nailed down. The one bit that had me stumped was numbering a set of div ids from top to bottom. I was able to grab your code, modify it, and get it working for me in under half an hour. (And only took that long because it took me a while to open the Output window to see my error message. D'oh.) I think I can now use the UE JS scripting for the whole process. +many – Anne Gunn Dec 31 '15 at 21:35