Can RegExReplace store subpatterns in output pseudo-arrays? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Guest

Can RegExReplace store subpatterns in output pseudo-arrays?

25 Jun 2017, 23:44

Can RegExReplace store subpatterns in output pseudo-arrays?

I'm trying to use backreferences ($1, $2, etc) in "chr()" function, but it doesn't work!

Using output pseudo-arrays (OutPutVar1, OutPutVar2, etc) doesn't work too!

Any solution?

Code: Select all

String := "--- 64 ---"

msgbox, % ""
. RegExReplace(String, "(64)", chr(64)) "`r`n"
. RegExReplace(String, "(64)", chr($1)) "`r`n"
. RegExReplace(String, "(64)", chr("$1")) "`r`n"
. RegExReplace(String, "(64)", "chr($1)") "`r`n"
. RegExReplace(String, "(64)", chr(OutputVar1)) "`r`n"
. RegExReplace(String, "(64)", chr("OutputVar1")) "`r`n"
. RegExReplace(String, "(64)", "chr(OutputVar1)") "`r`n"
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: Can RegExReplace store subpatterns in output pseudo-arrays?

26 Jun 2017, 01:56

Code: Select all

String := "--- 64 ---"

RegExMatch(String, "(64)", _)
MsgBox, % ""
. Chr(_) "`n"
. Chr(_1)

RegExMatch(String, "(?P<RTFM>64)", _)
MsgBox, % ""
. Chr(_) "`n"
. Chr(_RTFM)
Try to read the Help file first.
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Can RegExReplace store subpatterns in output pseudo-arrays?

26 Jun 2017, 08:54

IMEime wrote:Try to read the Help file first.
The question is about "RegExReplace" and you post a solution for "RegExMatch". Great.

I had already read the help file before posting this question here. I already knew how to do that with RegExMatch.

The question is, how to do the same with RegExReplace?

I need the "$1" backreference to be stored into a variable so I can use it with "Chr()" function inside RegExReplace.

Or even better, how to use "$1" backreference directly with the "Chr()" function inside RegExReplace?

Code: Select all

String := "--65--66--67--68--"

msgbox, % ""
. "--" Chr(65) "--" Chr(66) "--" Chr(67) "--" Chr(68) "--`r`n`r`n"
. RegExReplace(String, "(\d+)", "[$1]") "`r`n"
. RegExReplace(String, "(\d+)", Chr($1)) "`r`n"
. RegExReplace(String, "(\d+)", Chr("$1")) "`r`n"
. RegExReplace(String, "(\d+)", "Chr($1)") "`r`n"
. RegExReplace(String, "(\d+)", Chr(OutputVar1)) "`r`n"
. RegExReplace(String, "(\d+)", Chr("OutputVar1")) "`r`n"
. RegExReplace(String, "(\d+)", "Chr(OutputVar1)") "`r`n"

	;for the "Chr()" functions inside RegExReplace, the result should be, --A--B--C--D-- 



IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: Can RegExReplace store subpatterns in output pseudo-arrays?

26 Jun 2017, 09:18

i.m.p.o.s.s.i.b.l.e.
try to make code with possible style
Guest

Re: Can RegExReplace store subpatterns in output pseudo-arrays?  Topic is solved

26 Jun 2017, 09:26

You probably have to nest the REs

Code: Select all

var:=123
MsgBox % RegExReplace(RegExReplace(RegExReplace(var,"(3)","C$1"),"(2)","B$1"),"(1)","A$1")/code]
But I'm not sure what you want to do. Could you give a basic example of input and expected output as I suspect it will be possible but in an easier method as you suspect.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Can RegExReplace store subpatterns in output pseudo-arrays?

26 Jun 2017, 09:33

documetation @RegExReplace about the replacement parameters wrote: string to be substituted for each match, which is plain text (not a regular expression).
my scripts
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: Can RegExReplace store subpatterns in output pseudo-arrays?

26 Jun 2017, 09:44

IMEime wrote:i.m.p.o.s.s.i.b.l.e.
try to make code with possible style
IMEime: your red letters, sarcasm, and such aren't gaining favour or providing help. Not that I'm offended, but thought you might appreciate advice too.
try it and see
...
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Can RegExReplace store subpatterns in output pseudo-arrays?

26 Jun 2017, 09:44

IMEime wrote:i.m.p.o.s.s.i.b.l.e.
try to make code with possible style
Impossible?

Well, yet another silly limitation from AutoHotKey.

I hope in the next released versions, "OutputVar" for backreferences will be implemented in RegExReplace, as soon a subpattern is stored in "$1" backreference it will be stored too in "OutputVar1" variable, "$2" in "OutputVar2" and so on ...

Thanks for your time.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Can RegExReplace store subpatterns in output pseudo-arrays?

26 Jun 2017, 12:15

Code: Select all

outArr:=[]
String := "--65--66--67--68--"
RegExReplace(String, "(\d+)(?Cf)")
for k, v in outArr
	str.= v "`n" 
Msgbox, % str


f(m){
	global outArr
	outArr.push(chr(m))
	return 0
}
If you want pseudo array, modify to your liking.
Spoiler
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Can RegExReplace store subpatterns in output pseudo-arrays?

26 Jun 2017, 14:17

Helgef wrote:
Spoiler
Sorry for the late reply. That silly tough guy was disturbing and I didn't even noticed your post!

Your spoiler code is actually what I want (Though it is not straightforward.)

I tried this different approach just to certify if it would work or not, but it didn't work.

Code: Select all

Global $1
Global RegExOutputVar

String := "--65--66--67--68--"

msgbox, % ""
. RegExReplace(String, "(\d+)", Chr($1)) "`r`n"
. RegExReplace(String, "(\d+)(?CRegExReplaceCallOut)", Chr(RegExOutputVar)) "`r`n"
. "(" $1 " - " RegExOutputVar ")"

	;"?C" calls "RegExReplaceCallOut( )" function


RegExReplaceCallOut(Match)
{
RegExOutputVar := Match

	;msgbox, % RegExOutputVar
}
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Can RegExReplace store subpatterns in output pseudo-arrays?

26 Jun 2017, 15:06

The backreferences for the Replacement parameter are not output variables.
Maybe you'll find this more straight forward,

Code: Select all

String := "--65--66--67--68--"
p:=1
while (p:=RegExMatch(String, "(\d+)",m,p))
	String:=RegExReplace(String,m,chr(m),,1,p)
MsgBox, % String
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Can RegExReplace store subpatterns in output pseudo-arrays?

26 Jun 2017, 15:26

@User, are you used to a programming language that does RegExReplace and stores the old and/or new values in an array?

Also, I'd be interested in what this might be potentially useful for.

Btw although personally I don't want this functionality built into AHK, it is the sort of thing I might like to see in the documentation as an example (... or in my documentation extension tutorial, and RegEx tutorial).

Based on Helgef's code, is this anything like what you're looking for?

Code: Select all

q::
String := "--65--66--67--68--"
p:=1
outArr1:={}
outArr2:={}
while (p:=RegExMatch(String, "(\d+)",m,p))
{
	outArr1.push(m)
	outArr2.push(chr(m))
	String:=RegExReplace(String,m,chr(m),,1,p)
}
MsgBox, % String
str := ""
Loop, % outArr1.Length()
	str.= outArr1[A_Index] "`t" outArr2[A_Index] "`n"
Msgbox, % str
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Can RegExReplace store subpatterns in output pseudo-arrays?

26 Jun 2017, 19:37

Helgef wrote: .
jeeswg wrote: .
@Helgef, While + RegExMatch using StartPos + RegExReplace = worked wonderfully! (Very Straightforward!)

@jeeswg, below is a script example

# is a special character, ## represents a literal # character (it means that the first # escapes the second #)

#AnyNumber# is a special string (Example: #65# = A, #66# = B, in the other hand, ##65## = #65#, ##66## = #66#)

#AnyNumber# is automatically converted to its correspondent unicode character!

The script example works in real time, while typing or copy/pasting text, the output result is updated in real time!

May contain bugs, I don't know, the script may need more test!

Code: Select all

String := ""
. "--65--66--67--68-- __ --65666768--`r`n"
. "--##65##--##66##--##67##--##68##-- __ --##65####66####67####68##--`r`n"
. "--####65####--####66####-- __ --####65########66####--`r`n"
. "--#65#--#66#--#67#--#68#-- __ --#65##66##67##68#--`r`n"

gui, add, text, , Type or paste text below:
gui, add, edit, w450 H100 vEdit1 gUpdate WantTab, % String
gui, add, text, , OutPut:
gui, add, edit, w450 H100 vEdit2 +ReadOnly, % DecimalToChar(String)

gui, show
return

Update:	;__________ Update ______________

guicontrolget, Edit1

guicontrol, , Edit2, % DecimalToChar(Edit1)

return

guiclose:	;__________ Gui Close ______________
exitapp


DecimalToChar(Text)	;___________ DecimalToChar(Function) ________________
{
StartPos := 1

while, StartPos := RegExMatch(Text, "##(*SKIP)(*F)|#(\d+)#", Match, StartPos)
Text := RegExReplace(Text, "##(*SKIP)(*F)|" Match, Chr(Match1))

Text := RegExReplace(Text, "##", "#")

return, Text
}
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Can RegExReplace store subpatterns in output pseudo-arrays?

26 Jun 2017, 20:06

(*SKIP) and (*F) are interesting, and which I hadn't seen before.
pcresyntax specification
http://www.pcre.org/original/doc/html/pcresyntax.html

I really like the concept here, do you use a lot of Unicode characters? Myself, I often use hotstrings for the small number of Unicode characters I do use, e.g. sqrtx- alphax-. I'll be thinking over whether I have a use for this, although even the GUI presentation is quite nice and worth reusing.

It's quite a simple but effective system for escaping characters, is there some potential flaw in it ... choosing a good escape character system is often tricky. Is it your own system, or is it used elsewhere too? Might be useful for html/uri/url or related. Cheers.

Btw to test this effectively I have to have a few Unicode codepoints memorised!
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Can RegExReplace store subpatterns in output pseudo-arrays?

26 Jun 2017, 20:28

jeeswg wrote:(*SKIP) and (*F) are interesting, and which I hadn't seen before.
pcresyntax specification
http://www.pcre.org/original/doc/html/pcresyntax.html

I really like the concept here, do you use a lot of Unicode characters? Myself, I often use hotstrings for the small number of Unicode characters I do use, e.g. sqrtx- alphax-. I'll be thinking over whether I have a use for this, although even the GUI presentation is quite nice and worth reusing.

It's quite a simple but effective system for escaping characters, is there some potential flaw in it ... choosing a good escape character system is often tricky. Is it your own system, or is it used elsewhere too? Might be useful for html/uri/url or related. Cheers.

Btw to test this effectively I have to have a few Unicode codepoints memorised!
# as escaping character? I just chose it by chance.

Yes, I work on some small projects of mine that do heavy use of unicode characters!

Feel free to use the function the way you want, but notice that it needs to be tested and may be even buggy!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 336 guests