- AuthorPosts
- May 3, 2012 at 3:24 pm #10310userParticipant
hello!
can anyone help me achieve this please (I dont know if regex or macro or anything else is the appropriate way):
I have a text with many lines in this format:
GROUP1{TAB}ITEM1{TAB}SENT
GROUP1{TAB}ITEM2{TAB}SENT
GROUP2{TAB}ITEM3{TAB}SENT
GROUP2{TAB}ITEM4{TAB}NOT SENT
GROUP2{TAB}ITEM5{TAB}SENTGROUP1, GROUP2, etc are multidigit numbers
ITEM1, ITEM2, etc are any kind of characters sequence, including TABs, spaces, etc
the logic is that if in a GROUP, there is a single or more ITEM that is NOT SENT, then the whole GROUP must be marked as NOT SENT
if all items of a GROUP are SENT, then the GROUP is marked as SENT
so I want, with a click, to transform the above text into:
GROUP1{TAB}SENT
GROUP2{TAB}NOT SENTany solution?
thanks!
May 8, 2012 at 7:55 am #10335userParticipantanyone???
May 8, 2012 at 9:03 am #10336StefanParticipantuser wrote:
FROM:
GROUP1{TAB}IT{TAB}EM1{TAB}SENT
GROUP1{TAB}I{TAB}TE M2{TAB}SENT
GROUP2{TAB}IT E M3{TAB}SENT
GROUP2{TAB}ITE{TAB}M4{TAB}NOT SENT
GROUP2{TAB}ITEM5{TAB}SENTTO:
GROUP1{TAB}SENT
GROUP2{TAB}NOT SENTTry (with an copy of your file!)
this RegEx search & replace:FIND: (.+?t).+t(NOT SENT|SENT)
REPL: $1$2
[X] Use Regular ExpressionsExplanation:
(.+?t) = match and store all non-greedy till the first TAB into group $1
.+ = match all, but only till:
t(NOT SENT|SENT) = an TAB followed by “not sent” OR “sent”, which ever is stored in $2.
May 8, 2012 at 5:47 pm #10339userParticipantgreat thanks!
how do I count the instances of a match and store the amount in a variable?
May 8, 2012 at 6:36 pm #10340StefanParticipantuser wrote:
great thanks!how do I count the instances of a match and store the amount in a variable?
Do the RegEx s&r with an macro.
For example i have found that this
JavaScript macro could be an base for your issue:
//select your text, then execute the macro
if (document.selection.IsEmpty){alert('Nothing selected?'); quit();}
SelText = document.selection.text;
alert(SelText); // <== only for testing
//Settings:
MatchREx = "(.+?t).+t(NOT SENT|SENT)"; //Match/Find this
ReplWith = "$1$2"; //Replace with that
REModify = "gi"; //(G)lobal, (i)gnor case, (M)ultiline
//How many matches?
Matches = SelText.match(RegExp(MatchREx,REModify)).length;
//Do the RegEx search&replace itself:
ReplacedStr = SelText.replace(RegExp(MatchREx,REModify),ReplWith);
//The output:
alert(Matches + ' matches found'); // <== only for testing
alert(ReplacedStr); // <== only for testing
//Overwrite the selected text:
//document.selection.text = ReplacedStr;
- AuthorPosts
- You must be logged in to reply to this topic.