ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Main Area and Open Discussion > Living Room

Text editor - macro - multiple regex find and replaces

<< < (2/4) > >>

AbteriX:
Already good answers, here are some more....

You could use EmEditor Text Editor
- you can record a search&replace in a macro, edit that macro by copying the command line, and run this.
https://www.emeditor.com/text-editor-features/extensibility/scriptable-macros/
- you can also use the search&replace dialog box and enable Batch-Replace:
https://www.emeditor.com/text-editor-features/more-features/batch-replace/

You could use PowerShell (new WindowsTM batch tool)
Command line usage:
(Get-Content c:\temp\test.txt).replace('[MYID]', 'MyValue') | Set-Content c:\temp\test.txt
http://stackoverflow.com/questions/17144355/how-can-i-replace-every-occurence-of-a-string-in-a-file-with-powershell
Script usage:
$current=(Get-Content c:\temp\test.txt)
$OUT=$current.replace('[MYID]', 'MyValue')
$OUT=$OUT.replace('XXX', 'ABC')
$OUT | Set-Content c:\temp\test.txt

You could use AutoHotkey:
FileRead, varOrigin, test.csv
StringReplace, varOUT, varOrigin, xxx, ABC, All
StringReplace, varOUT, varOUT, YYY, DEF, All
FileDelete, test.csv
FileAppend, %varOUT%, test.csv
https://autohotkey.com/board/topic/66344-replace-in-text-file/

You could use MS-Word with VBA
- start record macro
- call search&replace , do your first replacement
- stop recording
- click Macros > Modify
- copy the replacement block of code and modify to your second replacement need
- save and execute
There are more elegent ways with ARRAYs and FOR EACH in Array DO... but for the start that should work for you too.

MS-Word supports Wildcards: http://word.mvps.org/FAQs/General/UsingWildcards.htm

Within a macro, you can utilize VBS/VBA RegEx (Set regExp = CreateObject("vbscript.regexp"))
http://superuser.com/questions/915285/how-to-construct-a-regex-find-replace-in-word-2010
http://stackoverflow.com/questions/26903163/word-find-and-replace-using-regex-alternation-operator

Best way is to copy a own (found) function into the macro code and call that function for the replacement work:
http://bytecomb.com/regular-expressions-in-vba/
>>> RxReplace VBA Function
Function RxReplace( SourceString, Pattern , ReplacePattern , IgnoreCase , MultiLine , MatchGlobal )


Just some ideas as starting point....


 

dcwul62:
First of all: MANY thanks for the feedback!
It is really appreciated.

after a lot of searching I think UltraEdit can do this.

one starts to record a macro, does 1 find and replace in regex
stop recording
save the macro and then add all extra find-and replaces on top.
probably it isn't vbscript as the macro looks very simple, but ... effective and easy to expand

InsertMode
ColumnModeOff
HexOff
PerlReOn
Find RegExp "^-bla-bla"
Replace All "$1-$2-$3"
Find RegExp "^bla bla"
Replace All "$4-$5-$6"
Find RegExp "bla bla"
Replace All "$7-$8-$9"

etc.

I have looked at all the above, except for the last reply from abterix. Still need to do that.
But believe UE is relatively straightforward, whereas the above suggestion do have a bit of a 'learning curve',  so to say.
UE issn't cheap though, I must think about it.

@AbteriX

Reverting on your feedback.

=


anandcoral:
Hi dcwul62,

I will like to add below which are specific for your purpose.
ultraedit http://www.ultraedit.com/ not free. I use it for the type of job as listed by you.

Further for search and replace with multiple times, below can do the job
powergrep https://www.powergrep.com/ not free
textcrawler https://www.digitalvolcano.co.uk/textcrawler.html has free version

Regards,

Anand

Note: As I was posting, I found your reply. So you found UltraEdit. Nevertheless I am posting my reply.

AbteriX:
A freeware Text Editor is PSPad, which can be scripted with VisualBasicScript (VBS)

Store that code as MultiReplace.vbs in "...\PSPad\Script\VBscript\" folder.
In PSPad run menu "Scripts\Recompile", next see new entry "Multiple Replace on downloaded text".


Quick&Dirty Example:

--- ---'*******************************************************************************
' Filename : X\PSPad\Script\VBscript\MultiReplace.vbs
' Description :
' Created :
' Created by :
' Note   :
' You may distribute this script freely, but please keep this header intact.
'*******************************************************************************
'OPTION EXPLICIT
'ON ERROR RESUME NEXT

Sub Init
        '//addMenuItem MODULE_Title, "sub menu" , MODULE_NAME_to_run ,"ctrl+shift+alt+m"
        addMenuItem "Multiple Replace on downloaded text", "" , "Main"
End Sub
'*******************************************************************************
Sub Main()
        Set editor = newEditor()
        editor.assignActiveEditor()
        strInputSel = editor.selText()


        strOut = strInputSel     
        '//==================================================== === START User Settings ===
         '//   strOut = runRegExpReplace(strOut, "yourSearchPattern" , "yourReplacement")
         strOut = runRegExpReplace(strOut, "X", "Y")
         strOut = runRegExpReplace(strOut, "-", "....")
         strOut = runRegExpReplace(strOut, "this", "that")
         strOut = runRegExpReplace(strOut, " the the ", " the ")
         '.... and so on
        '//==================================================== === END of User Settings ===

         '//write back:
        editor.selText strOut
        Set editor = Nothing
End Sub


Function runRegExpReplace(haystack, Pattern, newtext)
    Set regEx = New RegExp
    If Pattern = "" Then
        runRegExpReplace = haystack
        Exit Function
    End If
    regEx.Pattern        = Pattern
    regEx.IgnoreCase = TRUE
    regEx.Global          = TRUE
    regEx.Multiline      = TRUE
    runRegExpReplace = regEx.Replace(haystack, newtext)
End Function





.

dcwul62:
Abterix - thanks again! Appreciate this.
I checked out PSPad and did the following simple things.
existing text
start recording macro
do a find and replace
stop macro
save macro
(same to .pme ?)
then back to macro - edit macro - empty

tried a few times, but gave up.

Also tried the famous Notepad++ but that editor also doesn't seem to support editing macros.
At least not as simple as UltraEdit.
someone else also asked that
https://notepad-plus-plus.org/community/topic/13099/edit-a-marco-i-m-new-to-notepad
but as a new user one is referred to a rather complicated page that requires a lot of reading and stuff, so I dropped Notepad++

Then there are a lot of other editors like BBEdit, Sublime Text, Atom, etc. but those are focused on writing code, which is not for me.

There is still Notetab (Need to check it out)
EditPadPro is hardly ever mentioned.

=

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version