Delete only 1st word A in front of word B

Find, replace, find in files, replace in files, regular expressions

Delete only 1st word A in front of word B

Postby thomasm76 » Mon Jun 19, 2006 5:28 pm

Hello,

is it possible to do the following with (Perl)-RegEx?
Delete only the first special word A in front of another special word B and add a TAB(\t).

Before:
This is a wordA sentence with wordA followed by wordB
Another one with wordA and then wordB

After;
This is a wordA sentence with\tfollowed by wordB
Another one with\tand then wordB
User avatar
thomasm76
Basic User
Basic User
 
Posts: 19
Joined: Sun Jun 18, 2006 11:00 pm

Re: Delete only 1st word A in front of word B

Postby Mofi » Tue Jun 20, 2006 5:56 am

Don't know if it is possible with a Perl regex. But the following macro will do it independent of the the macro property Continue if a Find with Replace not found.

But only if there is always wordA before an occurence of wordB. If wordB exists also without wordA in the same line/paragraph/file? - you did not clearly describe that - it will search up for any wordA and replace it with a tab and so will replace all wordA by a tab before the first occurence of wordB without wordA.

InsertMode
ColumnModeOff
HexOff
Loop
Find MatchCase MatchWord "wordB"
IfFound
Find MatchCase MatchWord Up "wordA"
Replace "^t"
IfNotFound
ExitLoop
EndIf
Find MatchCase MatchWord "wordB"
Else
ExitLoop
EndIf
EndLoop
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4058
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Delete only 1st word A in front of word B

Postby thomasm76 » Tue Jun 20, 2006 8:16 am

Servus Mofi,

Actually there are SEVERAL LINES, each containing at least one wordA and one wordpartB. I want to replace wordA including the spaces before after wordA with a TAB.

Before:
Code: Select all
This is a wordA line with wordA followed by wordpartBandrestofword.
Another line with wordA and then wordpartBwithanotherrest.


After:
Code: Select all
This is a wordA line withTABfollowed by wordpartBandrestofword.
Another line withTABand then wordpartBwithanotherrest.


I just had to kick out "Matchword" of your Macro and it runs!
But shouldn't replace the Macro from first to last line of my file in one run, with "Run Macro once"? (cause of the endless loop in the Macro).
User avatar
thomasm76
Basic User
Basic User
 
Posts: 19
Joined: Sun Jun 18, 2006 11:00 pm

Re: Delete only 1st word A in front of word B

Postby Mofi » Tue Jun 20, 2006 8:33 am

thomasm76 wrote:I just had to kick out "Matchword" of your Macro and it runs!

Yes, you have to remove MatchWord and insert 2 spaces at the find string for wordA.

thomasm76 wrote:But shouldn't replace the Macro from first to last line of my file in one run, with "Run Macro once"? (cause of the endless loop in the Macro).

Yes, the macro replaces all wordA before wordB if you run it from top of the file. The macro does not contain the command Top. So it is executed from current cursor position till the position where wordB is found last (or " wordA " is not found upwards). There is no need to specify how often the macro should run and do the replacement.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4058
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Find/Replace/Regular Expressions

cron