RegExReplace

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
oldbrother
Posts: 265
Joined: 23 Oct 2013, 05:08

RegExReplace

Post by oldbrother » 29 Nov 2021, 19:01

I have a long text file with lot's of brackets like [abc ], [U12 3]. I can find them using:

RegExMatch(myTxt, "i)\[[0-9a-zA-Z\-_\s]*\]", Brac).

How can I use RegExReplace to remove the spaces inside of them?

"lot's of brackets like [abc ], [U12 3]" to: " lot's of brackets like [abc], [U123]"

User avatar
boiler
Posts: 16709
Joined: 21 Dec 2014, 02:44

Re: RegExReplace

Post by boiler » 29 Nov 2021, 19:26

Code: Select all

str := "lot's of brackets like [abc ], [U12 3]"
MsgBox, % RegExReplace(str, "\[[^\]]*?\K (?=.*])")
If your example is representative of the possible cases, then it works. Not good if you have more than one space between a pair of brackets.

User avatar
oldbrother
Posts: 265
Joined: 23 Oct 2013, 05:08

Re: RegExReplace

Post by oldbrother » 29 Nov 2021, 20:09

Thank you boiler,

Sorry for my example. The spaces can be more than one, or in multiple places within a pair of brackets:

Code: Select all

str := "lot's of brackets like [abc         ], [U  12  3], something like this [     D00 K]."

Target:

str := "lot's of brackets like [abc], [U123], something like this [D00K]."

User avatar
boiler
Posts: 16709
Joined: 21 Dec 2014, 02:44

Re: RegExReplace

Post by boiler » 29 Nov 2021, 20:23

It could be done with a loop, which would just keep repeating the above until it no longer replaces anything, but maybe someone will have a clever RegEx-only approach.

teadrinker
Posts: 4295
Joined: 29 Mar 2015, 09:41
Contact:

Re: RegExReplace

Post by teadrinker » 29 Nov 2021, 21:39

Code: Select all

str := "lot's of brackets like [abc         ], [U  12  3], something like this [     D00 K]."
MsgBox, % RegExReplace(str, "\s(?=[^[]*?])")

User avatar
oldbrother
Posts: 265
Joined: 23 Oct 2013, 05:08

Re: RegExReplace

Post by oldbrother » 30 Nov 2021, 08:28

It works perfectly! Thank you all! :thumbup:

Post Reply

Return to “Ask for Help (v1)”