Page 1 of 1
Can't delete text between "([[ ]])"
Posted: 21 Jan 2021, 15:20
by martin536
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)
Re: Can't delete text between "([[ ]])" Topic is solved
Posted: 21 Jan 2021, 15:50
by mikeyww
Code: Select all
str := "Roam [alias]([[Page]]) is useful for note taking."
new := RegExReplace(str, " *\[.+?\)")
MsgBox, 64, Result, %new%
Re: Can't delete text between "([[ ]])"
Posted: 21 Jan 2021, 15:51
by AlphaBravo
Code: Select all
RegExReplace(Clipboard, "\[\w*?\]\(\[\[\w*?\]\]\)\h+")
Re: Can't delete text between "([[ ]])"
Posted: 21 Jan 2021, 16:00
by mikeyww
Another one:
Code: Select all
new := RegExReplace(str, " *(\[.*?\]|\(.*?\))")
Re: Can't delete text between "([[ ]])"
Posted: 21 Jan 2021, 16:21
by HiSoKa
I tried too much to understand what's mean this symbols
, 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
Re: Can't delete text between "([[ ]])"
Posted: 21 Jan 2021, 16:28
by martin536
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%
Thank you
Re: Can't delete text between "([[ ]])"
Posted: 21 Jan 2021, 16:59
by mikeyww
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!
Re: Can't delete text between "([[ ]])"
Posted: 21 Jan 2021, 17:48
by martin536
HiSoKa wrote: ↑21 Jan 2021, 16:21
I tried too much to understand what's mean this symbols
, 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