Script for several consecutive predefined regex search/replace actions

Discuss and announce AkelPad plugins
Post Reply
  • Author
  • Message
Offline
Posts: 47
Joined: Sat Jul 05, 2008 11:30 am
Location: Odesa, Ukraine

Script for several consecutive predefined regex search/replace actions

Post by ewild »

Is there a script for several consecutive regex search/replace actions at a time without a GUI (search-replace pairs to be typed in by a user in a script itself)?

e.g.

Code: Select all

// *** Replace text in AkelPad regexp syntax;
// *** without GUI;
// *** in a single tab | in all tabs mode;

mode = active_tab | all_tabs

find_replace_pair_1
find =[\x{2010}\x{2011}\x{2012}\x{2013}\x{2014}\x{2212}]
replace =-

find_replace_pair_2
find =(\d{2})-(\d{2})-(\d{4})
replace =\3\2\1

...

find_replace_pair_N
find = ([09])\,([09])
replace = "\1.\2"

script {...}
Last edited by ewild on Fri Sep 06, 2024 10:51 am, edited 1 time in total.

Offline
Posts: 47
Joined: Sat Jul 05, 2008 11:30 am
Location: Odesa, Ukraine

Re: Script for several consecutive regex search/replace actions

Post by ewild »

No answers here so far, and finally I made one by myself (for the active tab only).

The script for several consecutive regex find/replace actions with the find/replace pairs predefined within the script.

Version A (line-by-line style):

Code: Select all

// predefined regex find replace line-by-line style
var hMainWnd=AkelPad.GetMainWnd();
if (hMainWnd)
{
nSelStart=AkelPad.GetSelStart();
nSelEnd=AkelPad.GetSelEnd();
if (nSelStart == nSelEnd)
AkelPad.SetSel(0,-1);
pSelText=AkelPad.GetSelText();

pSelText=pSelText.replace(/AAA/g,"BBB");
pSelText=pSelText.replace(/CCC/g,"DDD");
pSelText=pSelText.replace(/EEE/g,"FFF");
pSelText=pSelText.replace(/(\d{2})-(\d{2})-(\d{4})/g,"$3-$2-$1");

AkelPad.ReplaceSel(pSelText);
AkelPad.SetSel(nSelStart,nSelStart);
}
Version B (hash-table style):

Code: Select all

// predefined regex find replace hash-table style
var hMainWnd=AkelPad.GetMainWnd();
if (hMainWnd)
{
nSelStart=AkelPad.GetSelStart();
nSelEnd=AkelPad.GetSelEnd();
if (nSelStart == nSelEnd)
AkelPad.SetSel(0,-1);
pSelText=AkelPad.GetSelText();

var hashTable = {
"AAA":"BBB",
"CCC":"DDD",
"EEE":"FFF",
"(\\d{2})-(\\d{2})-(\\d{4})":"$3-$2-$1"
};

for(var hashKey in hashTable)
pSelText=pSelText.replace(new RegExp(hashKey,"g"),hashTable[hashKey]);

AkelPad.ReplaceSel(pSelText);
AkelPad.SetSel(nSelStart,nSelStart);
}
Text to check how the example script works:

Code: Select all

AAA = BBB
CCC = DDD
EEE = FFF

05-09-2024

Offline
Posts: 47
Joined: Sat Jul 05, 2008 11:30 am
Location: Odesa, Ukraine

Re: Script for several consecutive regex search/replace actions

Post by ewild »

And going further with the .TextReplace() method:

for the active file only:

Code: Select all

var hashTable = {
"AAA":"BBB",
"CCC":"DDD",
"EEE":"FFF",
"(\\d{2})-(\\d{2})-(\\d{4})":"\\3-\\2-\\1"
};
for(var hashKey in hashTable)
AkelPad.TextReplace(0,hashKey,hashTable[hashKey],0x00280000,0x1);
for all the opened files:

Code: Select all

var hashTable = {
"AAA":"BBB",
"CCC":"DDD",
"EEE":"FFF",
"(\\d{2})-(\\d{2})-(\\d{4})":"\\3-\\2-\\1"
};
for(var hashKey in hashTable)
AkelPad.TextReplace(0,hashKey,hashTable[hashKey],0x01280000,0x1);
Notes:
  • \\ double escaping required in case of regex
  • AkelPad.TextReplace() syntax (see AkelFiles\Docs\Scroll-Eng.txt):
    TextReplace(hHandle, pFindIt, pReplaceWith, nFindFlags, nReplaceFlags);
    nFindFlags:
    0x01280000 mode : regex, all files
    0x00280000 mode : regex, active file, from beginning
Post Reply