0

I use Perl regex in UltraEdit to find and replace text with no problem. But lately, I've needed a lot to replace digits (numbers) I find with another number generated from the found number by applying mathematical operations. For example, I search for a three digits number \d\d\d, let's say I found 264. What I need is to replace the found number with another number by adding, for example, 5, then it should be replaced with 269. Thanks in advance!

1
  • StackOverflow is for programming questions. Questions about how to use your editor belong on Super User.
    – ikegami
    May 18, 2014 at 22:29

3 Answers 3

3

In Perl, you can use the /e ("evaluate") modifier:

s/(\d\d\d)/$1+5/e

I'm not sure about its availability in UltraEdit, though.

2
  • I used Find: (\d\d\d) and Replaced with: s/(\d\d\d)/$1+5/e. But the result was s/(ddd)/264+5/e, not 269.
    – Omar
    May 18, 2014 at 21:44
  • @user3503851 , s/// is Perl operator. Makes no sense to use it as the replacement expression. Only $1+5 is the replacement expression. But of course, there's no equivalent to e in UE.
    – ikegami
    May 18, 2014 at 22:25
1

UltraEdit does not have give the capability to execute code (of any kind) to generate the replacement value.

You could easily use perl to do the task, though.

perl -i.bak -e"s/(\d\d\d)/$1+5/eg" file

Or if you want to use wildcards,

perl -MFile::DosGlob=glob -i.bak -e"BEGIN { @ARGV = map glob($_), @ARGV } s/(\d\d\d)/$1+5/eg" *.txt
0

UltraEdit has the capability to execute code as UltraEdit has a scripting support since version 13.00 which uses the JavaScript core engine.

Here is an UltraEdit script for searching in a file for all numbers with exactly 3 digits and increase all found numbers by a value of 5.

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();
   // Define the parameters for the Perl regular expression find.
   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 in a loop for numbers with exactly 3 digits, convert each
   // found number as string selected in the active file to a number,
   // increment the number by 5 and write the number converted back
   // to string over the found number string.
   while(UltraEdit.activeDocument.findReplace.find("(?<!\\d)\\d{3}(?!\\d)"))
   {
      var nNumber = parseInt(UltraEdit.activeDocument.selection,10);
      nNumber += 5;
      UltraEdit.activeDocument.write(nNumber.toString(10));
   }
   // Move caret back to top of the file.
   UltraEdit.activeDocument.top();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.