Can't delete text between "([[ ]])" Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
martin536
Posts: 6
Joined: 21 Jan 2021, 14:59

Can't delete text between "([[ ]])"

Post by martin536 » 21 Jan 2021, 15:20

I've read that RegExEsc("([[") is useful for this case, but I can't get it to work.

My goal is to convert this:
Roam [alias]([[Page]]) is useful for note taking.
to:
Roam is useful for note taking.
[alias]([[Page]]) gone.

So far I tried:

Code: Select all

Msgbox, % RegExReplace(Clipboard, U+005B, "")
and

Code: Select all

RegExReplace(Clipboard, "\([[\w+]])")
and

Code: Select all

beginning = RegExEsc("([[")
    RegExReplace(Clipboard, beginning)
Last edited by gregster on 21 Jan 2021, 15:39, edited 1 time in total.
Reason: Formatting fixed.
User avatar
mikeyww
Posts: 5357
Joined: 09 Sep 2014, 18:38

Re: Can't delete text between "([[ ]])"  Topic is solved

Post by mikeyww » 21 Jan 2021, 15:50

Code: Select all

str := "Roam [alias]([[Page]]) is useful for note taking."
new := RegExReplace(str, " *\[.+?\)")
MsgBox, 64, Result, %new%
Last edited by mikeyww on 21 Jan 2021, 15:53, edited 1 time in total.
User avatar
AlphaBravo
Posts: 513
Joined: 29 Sep 2013, 22:59

Re: Can't delete text between "([[ ]])"

Post by AlphaBravo » 21 Jan 2021, 15:51

Code: Select all

RegExReplace(Clipboard, "\[\w*?\]\(\[\[\w*?\]\]\)\h+")
User avatar
mikeyww
Posts: 5357
Joined: 09 Sep 2014, 18:38

Re: Can't delete text between "([[ ]])"

Post by mikeyww » 21 Jan 2021, 16:00

Another one:

Code: Select all

new := RegExReplace(str, " *(\[.*?\]|\(.*?\))")
User avatar
HiSoKa
Posts: 109
Joined: 27 Jan 2020, 15:43

Re: Can't delete text between "([[ ]])"

Post by HiSoKa » 21 Jan 2021, 16:21

I tried too much to understand what's mean this symbols

Code: Select all

*(\[.*?\]|\(.*?\))"
, i didn't found it in a documentation ,, for example now we want to delete only () and [] but now why have to put all this symbol to delete only this 4 symbols , can anyone give me more explanation about it please ..
thanks in advance
martin536
Posts: 6
Joined: 21 Jan 2021, 14:59

Re: Can't delete text between "([[ ]])"

Post by martin536 » 21 Jan 2021, 16:28

mikeyww wrote:
21 Jan 2021, 15:50

Code: Select all

str := "Roam [alias]([[Page]]) is useful for note taking."
new := RegExReplace(str, " *\[.+?\)")
MsgBox, 64, Result, %new%
:D Thank you
User avatar
mikeyww
Posts: 5357
Joined: 09 Sep 2014, 18:38

Re: Can't delete text between "([[ ]])"

Post by mikeyww » 21 Jan 2021, 16:59

I'm glad it works!

Hello, @HiSoKa, you might have looked in the wrong section. Reference is below.

https://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm

We are deleting not only the brackets but their contents, so that is where RegEx can be useful, as the contents may be unknown in advance. RegEx looks for patterns instead of literal strings, and the patterns simply have their own syntax like everything else. The backslashes are needed to escape the characters, since the brackets otherwise have special meaning for RegEx. In many cases, Instr or SubStr are alternatives to certain approaches with RegEx. They aren't better or worse, just different ways to solve the same problem.

If you find a better way, please feel free to post any additional solutions!
martin536
Posts: 6
Joined: 21 Jan 2021, 14:59

Re: Can't delete text between "([[ ]])"

Post by martin536 » 21 Jan 2021, 17:48

HiSoKa wrote:
21 Jan 2021, 16:21
I tried too much to understand what's mean this symbols

Code: Select all

*(\[.*?\]|\(.*?\))"
, i didn't found it in a documentation ,, for example now we want to delete only () and [] but now why have to put all this symbol to delete only this 4 symbols , can anyone give me more explanation about it please ..
thanks in advance
You want to focus on *(\[.*?\]|\(.*?\))"

Code: Select all

* >> Have no idea.
.*? >> I asume it means all in between. https://www.autohotkey.com/docs/commands/_EscapeChar.htm
| >> (OR)
\ >> acknowledges the special characters such as "[" or "]" or      \.*?+[{|()^$
If you want to preserve the content of "( )". You could use.

Code: Select all

    StringReplace Clipboard, Clipboard, (,, All; 				; this will delete all the "("
    StringReplace Clipboard, Clipboard, ),, All;				; this will delete all the ")"
I'm using this for Roam Research.
This script retains the content between "[ ]" and gets rid of alias references "([[text]])"

Code: Select all

+!n::										; this script retains the content between "[ ]" and gets rid of alias references "([[text]])"
    clipsaved:= ClipboardAll
    Clipboard := ""
    Send, ^c
    ClipWait								; now you copied the highlighted text
    new := RegExReplace(Clipboard, " *\(\[\[.+?\]\]\)")   ; this will delete all the alias page references "([[ ]])" and it's content (meaning text)
    StringReplace new, new, [,, All; 				; this will delete all the "["
    StringReplace new, new, ],, All;				; this will delete all the "]"
    MsgBox, 64, Result, %new%					; this will show you the result. You can delete if you don't need it.
    Clipboard := new
RETURN
:roll:
Post Reply

Return to “Ask For Help”