The script code to renumber the DIV identifiers:
Code: Select all
if (UltraEdit.document.length > 0) // Is any file opened?
{
// Define environment for this script.
UltraEdit.insertMode();
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 the parameters for a case-sensitive Perl regular expression
// find in current file from top of the file to end of the file.
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;
}
// Define the first number decrement by one on each found occurrence.
var nNumber = 401;
// Search in a loop with the Perl regular expression <div[\t ]+id=(["'])a\K\d+(?=\1)
// for starting DIV tags with first attribute being the attribute id with
// name starting with character a and having next one or more digits and
// no other characters up to end of the name. Matched is only the number.
while (UltraEdit.activeDocument.findReplace.find("<div[\\t ]+id=([\"'])a\\K\\d+(?=\\1)"))
{
// Decrement the number.
nNumber--;
// Overwrite the matched (selected) number with the current
// number converted to a string using decimal system.
UltraEdit.activeDocument.write(nNumber.toString(10));
}
}
The script was tested on the input data (missing </div> tags added):
Code: Select all
"Lorem ipsum dolor sit amet <div id="a401" class="capture">, consectetur adipiscing elit, sed do eiusmod tempor incidi </div><div id="a401" class="capture"> dunt ut labore et dolore magna aliqua. Ut en </div><div id="a401" class="capture"> im ad minim veniam, quis nostrud exercitation ulla </div><div id="a401" class="capture"> mco laboris nisi ut aliquip ex ea commodo conse </div><div id="a401" class="capture"> quat. Duis aute irure dolor in reprehenderit in volup </div><div id="a401" class="capture"> tate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occa </div><div id="a401" class="capture"> ecat cupidatat non proident, sunt in culpa qui offi </div><div id="a401" class="capture"> cia deserunt mollit anim id est laborum.</div>
The output result with the script above is:
Code: Select all
"Lorem ipsum dolor sit amet <div id="a400" class="capture">, consectetur adipiscing elit, sed do eiusmod tempor incidi </div><div id="a399" class="capture"> dunt ut labore et dolore magna aliqua. Ut en </div><div id="a398" class="capture"> im ad minim veniam, quis nostrud exercitation ulla </div><div id="a397" class="capture"> mco laboris nisi ut aliquip ex ea commodo conse </div><div id="a396" class="capture"> quat. Duis aute irure dolor in reprehenderit in volup </div><div id="a395" class="capture"> tate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occa </div><div id="a394" class="capture"> ecat cupidatat non proident, sunt in culpa qui offi </div><div id="a393" class="capture"> cia deserunt mollit anim id est laborum.</div>"
Here is one more script which reverses the DIV elements in the file containing the input data as posted above.
Code: Select all
if (UltraEdit.document.length > 0) // Is any file opened?
{
// Define environment for this script.
UltraEdit.insertMode();
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 the parameters for a just once executed case-sensitive
// Perl regular expression find in current file from top of the file.
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 with the Perl regular expression (?:<div[\\t ]+id=([\"'])a\\d+(?=\\1)[\\s\\S]+?</div>\\s*)+
// for one or more DIV elements with first attribute being the attribute id
// with name starting with character a and having next one or more digits
// and no other characters up to end of the name. There can be whitespaces
// between the DIV elements. But the DIV elements to reorder must be in
// one block not being too large to select them with a Perl regex find.
if (UltraEdit.activeDocument.findReplace.find("(?:<div[\\t ]+id=([\"'])a\\d+(?=\\1)[\\s\\S]+?</div>\\s*)+"))
{
// Get the selected DIV elements as an array of strings.
var asElements = UltraEdit.activeDocument.selection.split("</div>");
// Remove the last string which is always an empty string.
asElements.pop();
// Reverse the elements in the array.
asElements.reverse();
// Append an empty string to the array.
asElements.push("");
// Join the string of the array together and overwrite the
// selected block of DIV elements with the reversed elements.
UltraEdit.activeDocument.write(asElements.join("</div>"));
}
}
The output of this little script is:
Code: Select all
"Lorem ipsum dolor sit amet <div id="a401" class="capture"> cia deserunt mollit anim id est laborum.</div><div id="a401" class="capture"> ecat cupidatat non proident, sunt in culpa qui offi </div><div id="a401" class="capture"> tate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occa </div><div id="a401" class="capture"> quat. Duis aute irure dolor in reprehenderit in volup </div><div id="a401" class="capture"> mco laboris nisi ut aliquip ex ea commodo conse </div><div id="a401" class="capture"> im ad minim veniam, quis nostrud exercitation ulla </div><div id="a401" class="capture"> dunt ut labore et dolore magna aliqua. Ut en </div><div id="a401" class="capture">, consectetur adipiscing elit, sed do eiusmod tempor incidi </div>"
The id attributes are not removed, but that could be easily done with a replace before splitting up the selected blocks into DIV elements. It would be of course also possible to output the reversed elements in a better format like inserting indenting spaces/tabs and line breaks.