You are here: PSPad forum > English discussion forum > regular expressions

regular expressions

#1 regular expressions

Posted by: davek | Date: 2012-09-04 22:02 | IP: IP Logged

I have downloaded PSPad to perform automatic edits to programs which run machine tools where I work.

I need to make a macro using search and replace to take all one or two digit numbers after a "T" and move them after the next occurrence of "M6" (with a "T").

Example

T77
G90G55G17G0X33.138Y36.944
G43Z25.H76S300M3
Z25.M8
G1Z-17.5F1500.
X34.879Y27.071F90.
M6

needs to end up as

T77
G90G55G17G0X33.138Y36.944
G43Z25.H76S300M3
Z25.M8
G1Z-17.5F1500.
X34.879Y27.071F90.
M6T77

The text in between the T** could be pages long.

There will be no other "T"s and no occurrences of "M" without a "6" after.

No lower case letters

In word I did this with

find <T(*)(*)>*M6
replace ^&T\1\2

The * referred to all the intervening text nicely.

I can't seem to do this with regular expressions. Help me Mr. Wizard.

Options: Reply | Quote | Up ^


#2 Re: regular expressions

Posted by: vbr | Date: 2012-09-05 10:03 | IP: IP Logged

davek:
I have downloaded PSPad to perform automatic edits to programs which run machine tools where I work.

I need to make a macro using search and replace to take all one or two digit numbers after a "T" and move them after the next occurrence of "M6" (with a "T").

Example

T77
G90G55G17G0X33.138Y36.944
G43Z25.H76S300M3
Z25.M8
G1Z-17.5F1500.
X34.879Y27.071F90.
M6

needs to end up as

T77
G90G55G17G0X33.138Y36.944
G43Z25.H76S300M3
Z25.M8
G1Z-17.5F1500.
X34.879Y27.071F90.
M6T77

The text in between the T** could be pages long.

There will be no other "T"s and no occurrences of "M" without a "6" after.

No lower case letters

In word I did this with

find <T(*)(*)>*M6
replace ^&T\1\2

The * referred to all the intervening text nicely.

I can't seem to do this with regular expressions. Help me Mr. Wizard.

Hi,
You can't do such conversion using the built in PSPad functionality, but you can use a script which performs a regular expression replacement via javascript.
You can try the following one and adjust it as needed (Copy the source to a file, e.g.
...\PSPad\Script\JScript\test_js.js
and possibly enable WSH scripting in Settings: System integration
You can call the script from the Scripts menu, or with a shortcut (adjustable in the script - Init)

The pattern also requires that "M6" is originally at the end of the line or the whole file; if it isn't the case in your data, just remove (?=(?:\r\n)|$) but in this case the repeated calls of this replacement will probably corrupt the data and copy the T.. codes across the original blocks.

You should test the script carefully, whether it suits your needs - as a javascript, it might have problems on large files; unicode characters over the current ANSII codepage are lost with such replacement.

hth,
vbr

Quote:

//// ... PSPad\script\JScript\copy_T_nr.js ///
var module_name = "copy_T_nr";
var module_ver = "1";

function copy_T_nr(){
if (editorsCount()<1){return;} // quit if no text window is open
var actEd = newEditor();
actEd.assignActiveEditor();
var inputTxt = actEd.text(); // get the whole text
var outputTxt = inputTxt.replace(/(T\d{1,2})((?:.|\n)*?)(M6(?=(?:\r\n)|$))/g, "$1$2$3$1");
actEd.text(outputTxt); // set the modified text content
}

function Init(){
addMenuItem("copy T nr", "", "copy_T_nr", "Ctrl+Alt+Shift+Q");// labels and shortcuts can be modified
}

Options: Reply | Quote | Up ^






Editor PSPad - freeware editor, © 2001 - 2024 Jan Fiala, Hosted by Webhosting TOJEONO.CZ, design by WebDesign PAY & SOFT, code Petr Dvořák, Privacy policy and GDPR