RegExReplace and some characters

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

RegExReplace and some characters

02 Aug 2016, 00:51

I strugle to decribe these characters correctly like code give me normal value as normal characters. Can anybode help me With this RegExReplace?

Code: Select all

ab := "show these characters as normal characters :,!#¤%&/(()))=%`\'*-/+\.*?+[{|()^$"
adds := RegExReplace(ab, "", "")
msgbox, %adds%
return
Guest

Re: RegExReplace and some characters

02 Aug 2016, 05:14

You can escape special characters by preceding them with a \ so if you want search for literal ( you write it like so: \(
p3trus
Posts: 137
Joined: 02 Aug 2016, 03:20

Re: RegExReplace and some characters

02 Aug 2016, 05:48

...this RegExReplace does nothing, you know? You're replacing the empty string with the empty string.

I don't really understand your problem, but one possible problem is the default escape character (of ahk), ` - to get a literal `, you have to double it `` (commands/_EscapeChar.htm)
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Re: RegExReplace and some characters

02 Aug 2016, 06:41

I solved it like

Code: Select all

NewStr1 := RegExReplace(pe1, "\R", "``n") 
NewStr2 := RegExReplace(NewStr1, "\t", "``t")
NewStr5 := RegExReplace(NewStr2, "\{", "(")
NewStr6 := RegExReplace(NewStr5, "\}", ")")
NewStr7 := RegExReplace(NewStr6, "#", "{#}")
NewStr8 := RegExReplace(NewStr7, "\|", "/")
NewStr9 := RegExReplace(NewStr8, "\+","{+}")
NewStr10 := RegExReplace(NewStr9, "\^", "{^} ")
NewStr11 := RegExReplace(NewStr10, "!", "{!}")
NewStr12 := RegExReplace(NewStr11, ";", "```;")
but it doesn't looks Nice in code. But what to do, I think it must stay like that.
p3trus
Posts: 137
Joined: 02 Aug 2016, 03:20

Re: RegExReplace and some characters

02 Aug 2016, 07:17

..if you try to explain what you want exactly, maybe I (or someone else) can help out.
Guest

Re: RegExReplace and some characters

02 Aug 2016, 07:49

You don't need to create a new string variable each time you can just keep on assigning it to the same variable:

Code: Select all

NewStr := RegExReplace(NewStr, "\t", "``t")
NewStr := RegExReplace(NewStr, "\{", "(")
; etc
Alternative method (you can write the object as one line, multiple lines here for readability:

Code: Select all

SearchObject:={ "\R" : "``n"
	, "\t" : "``t"
	, "\{" : "("  
	, "\}" : ")"  
	, "\|" : "/"  
	, ";": "```;"
	, "(#|\+|\^|!)" : "{$1}" }
	
for k, v in SearchObject
	NewStr:=RegExReplace(NewStr,k,v)
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Re: RegExReplace and some characters

03 Aug 2016, 01:21

Guest wrote: Alternative method (you can write the object as one line, multiple lines here for readability:

Code: Select all

SearchObject:={ "\R" : "``n"
	, "\t" : "``t"
	, "\{" : "("  
	, "\}" : ")"  
	, "\|" : "/"  
	, ";": "```;"
	, "(#|\+|\^|!)" : "{$1}" }
	
for k, v in SearchObject
	NewStr:=RegExReplace(NewStr,k,v)
alternative Method looks Nice, but how to describe ` part if I want get the same value? SearchObject:={ "" : ""}
p3trus
Posts: 137
Joined: 02 Aug 2016, 03:20

Re: RegExReplace and some characters

03 Aug 2016, 06:11

euras wrote:alternative Method looks Nice, but how to describe ` part if I want get the same value? SearchObject:={ "" : ""}
You don't need it at all, since replacing "anyvalue" by "thesamevalue" leaves the original string unchanged.
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: RegExReplace and some characters

03 Aug 2016, 08:07

Code: Select all

ab :=	abc :=	"show these characters as normal characters :,!#¤%&/(()))=%`\'*-/+\.*?+[{|()^$" ; make a copy of the string to search alongside the original
Pos=1 ; set the starting position for RegExMatch in the While loop
While	Pos :=	RegExMatch(abc,"\W",m,Pos+StrLen(m)) ; while non-word characters are found in the copy of the original string
	if	!(m~="\s") && (ab~="\" m) ; if the found character is not a whitespace character and exists in the original string
		MsgBox %	(ab :=	RegExReplace(ab,"\" m)) ; remove it from the original string
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Aqualest, ComeOrDream and 363 guests