How to translate javascript regex to AHK ?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mojobadshah
Posts: 25
Joined: 12 Nov 2019, 16:32

How to translate javascript regex to AHK ?

19 Nov 2019, 13:11

I have coded a javascript + regex input function like this:


Code: Select all

<input type="button" onclick="
document.getElementById('example').value = document.getElementById('example').value.replace(/([<])/gmi,'\n\n\n$&');" value="EXAMPLE">

and I would like to recode this so that it works as an Autohotkey (AHK) regex function. The AHK RegExReplace function I have put together so far is this:


Code: Select all

ControlGetText, data, Edit1, Untitled - Notepad ; 

data:=  RegExReplace("???", "???", "???")  ;

FileAppend,  %data%, Untitled.txt


What do I need to do to make the first js regex instance (a) .replace(/([<])/gmi,'\n\n\n$&') accurately correspond to ahk regex (b) RegExReplace("???", "???", "???")
nitrofenix
Posts: 18
Joined: 20 Sep 2019, 03:26

Re: How to translate javascript regex to AHK ?

19 Nov 2019, 18:57

I'm not familiar with JavaScript regex, but ran it through an online regex tester and came up with this:

Code: Select all

data := RegExReplace(data, "([<])", "\n\n\n$1")
mojobadshah
Posts: 25
Joined: 12 Nov 2019, 16:32

Re: How to translate javascript regex to AHK ?

20 Nov 2019, 00:06

Neither \n or 'n is a recognized regex expression in this setting for rendering a newline in a character string. What regex expression should I use to form a newline in a string of text?
User avatar
Chunjee
Posts: 1498
Joined: 18 Apr 2014, 19:05
Contact:

Re: How to translate javascript regex to AHK ?

20 Nov 2019, 07:21

mojobadshah wrote:
20 Nov 2019, 00:06
Neither \n or 'n is a recognized regex expression in this setting for rendering a newline in a character string. What regex expression should I use to form a newline in a string of text?
\n is acceptable. https://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm also mentions ") Circumflex (^) matches immediately after all internal newlines" when using the Multiline option, which is done at the start of the pattern unlink js which is done after the / at the end of the pattern.

https://regex101.com/r/FCd6q5/2
which would be the following in ahk: "m)\w*\s\w*\n\w*"

Notepad/Windows often saves newlines as \n\r so that often trips people up. I usually convert all \r to \n in memory before performing regex as a way to keep regex patterns from getting too complex.
mojobadshah
Posts: 25
Joined: 12 Nov 2019, 16:32

Re: How to translate javascript regex to AHK ?

20 Nov 2019, 14:12

Chunjee wrote:
20 Nov 2019, 07:21
mojobadshah wrote:
20 Nov 2019, 00:06
Neither \n or 'n is a recognized regex expression in this setting for rendering a newline in a character string. What regex expression should I use to form a newline in a string of text?
\n is acceptable. https://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm also mentions ") Circumflex (^) matches immediately after all internal newlines" when using the Multiline option, which is done at the start of the pattern unlink js which is done after the / at the end of the pattern.

https regex101.com /r/FCd6q5/2 Broken Link for safety
which would be the following in ahk: "m)\w*\s\w*\n\w*"

Notepad/Windows often saves newlines as \n\r so that often trips people up. I usually convert all \r to \n in memory before performing regex as a way to keep regex patterns from getting too complex.
This is the only thing that makes sense to me in terms of your response. The first two instances (and generally a lot of ahk regex don't appear to correspond to js regex very realistically). My presumption in respects to the last option is that this option speaks to having to write a ahk regex script that is much more expansive where \n is not actually recognized as a regex expression, rather \n could really be interchangeable with any real character (eg. regular text) - it's really a 'hashtag' that is what is being identified in the last option

{

Data := RegExReplace(Data, "([<])","\n$1")

}

[/code]


So, the way things are scripted now something like 'Mary<John<Mary' in a text string will resolve to 'Mary\n<John\n<Mary,' and I can see how I can implement another function similar to the one directly above, but a bit more complicated. Haven't mastered the technique, but I noticed something that involved a "Loop, Parse" effort like this

Code: Select all

Loop, Parse, Data, `n, `r
{

	if RegExMatch(A_LoopField, "(\w+)", Match )

		
	Data .= A_LoopField "`n"
}   

And this almost works in reference to the indicated 3rd option to make use of extra characters eg. \n (which would otherwise service directly as a ahk expression, normally represented as 'n within ahk's coding language system) as substitute hashtags. My question in relation to this method is - is this the only way to actually 'add' newlines into a string of text, and if so how do I make this 'Loop, Parse' function conform to the RegExReplace function I've provided above as a 2nd sequence? Otherwise, what do I add to the initial RegExReplace function that I provided so that \n does not resolve to actual \n in ahk results, but rather pushes or splits text to form an actual newline in a text?
User avatar
Chunjee
Posts: 1498
Joined: 18 Apr 2014, 19:05
Contact:

Re: How to translate javascript regex to AHK ?

20 Nov 2019, 23:58

mojobadshah wrote:
20 Nov 2019, 14:12
\n is not actually recognized as a regex expression, rather \n could really be interchangeable with any real character (eg. regular text) - it's really a 'hashtag' that is what is being identified in the last option

And this almost works in reference to the indicated 3rd option to make use of extra characters eg. \n (which would otherwise service directly as a ahk expression, normally represented as 'n within ahk's coding language system) as substitute hashtags. My question in relation to this method is - is this the only way to actually 'add' newlines into a string of text
The 2nd parameter is the regex pattern. \n is valid there. the 3rd parameter is what you want the replacement to be. `n would be a newline in that case. This can be proven be writing the new variable to a file. in which case it will be a newline and not a "`n" string.


I had too lookup the significance of $& and didn't quite understand why it would be used when seemingly all you are capturing is ">"

Code: Select all

file =
(
    Mary<John<Mary

    couple newlines
    end
)

NewStr := RegExReplace(file, "([<])", "`n`n`n$1")
FileAppend, %NewStr%, % A_ScriptDir "\text.txt"
ExitApp, 1

Code: Select all

Mary


<John


<Mary

    couple newlines
    end
User avatar
Chunjee
Posts: 1498
Joined: 18 Apr 2014, 19:05
Contact:

Re: How to translate javascript regex to AHK ?

21 Nov 2019, 00:05

Code: Select all

NewStr := RegExReplace(file, "\n", "[formerly newline]")
; => Mary<John<Mary[formerly newline][formerly newline]couple newlines[formerly newline]end

I don't really understand what is being attempted with the loop, parse portion of your question. I prefer StrSplit()
mojobadshah
Posts: 25
Joined: 12 Nov 2019, 16:32

Re: How to translate javascript regex to AHK ?

21 Nov 2019, 13:45

Chunjee wrote:
21 Nov 2019, 00:05

Code: Select all

NewStr := RegExReplace(file, "\n", "[formerly newline]")
; => Mary<John<Mary[formerly newline][formerly newline]couple newlines[formerly newline]end

I don't really understand what is being attempted with the loop, parse portion of your question. I prefer StrSplit()
Thank you for your response. Your example:

Code: Select all

file =
(
    Mary<John<Mary

    couple newlines
    end
)

NewStr := RegExReplace(file, "([<])", "`n`n`n$1")
FileAppend, %NewStr%, % A_ScriptDir "\text.txt"
ExitApp, 1
effectively demonstrated how to capture a literal char and push that character to a \n (and I see how 'n isn't exactly expressive of a newline, but a newstring, now). What is also appreciative of your method here is the fact that the FileAppend in your script does not add a reiteration of the old text, but replaces it completely. Having to attack the data located inside an ahk script, rather than attacking the data located inside a text file becomes, tentatively, a semantic question for me.... In reference to your above suggestion and a preference for 'StrSplit' I'm a little divided in terms of which parameters require substitution to make an almost 100% effective ahk RegExReplace system that can be applied to strings of text located inside a txt file and am struggling to keep this ahk script together:

Code: Select all


ControlGetText, Data, Edit1, Untitled - Notepad ; 
{
Data := RegExReplace(Data, "im)([<])","\n$1")  		
}
FileAppend,  %Data%, Untitled.txt


So I can formulate some clarity... 1.) the FileAppend code you showed me above that initializes with with a "file =" statement - is this an indication (maybe also alluded to in previous posts... ) that in order to perform an 'ahk RegExReplace function' with operator ' \n ' this is only generative when data is actually contexted in an ahk script, or should I be able to apply precision to the 'ahk RegExReplace function' that initializes with a 'ControlGetText' header that works in direct application to 'Data' located in a Notepad file 'Untitled.txt,' by maybe substituting a ' \n ' ahk expression and operator with an ahk expression with 'StrSplit' ?
User avatar
Chunjee
Posts: 1498
Joined: 18 Apr 2014, 19:05
Contact:

Re: How to translate javascript regex to AHK ?

21 Nov 2019, 16:01

I think you should just post the file you are working with and the ideal output. I'm not understanding most of your posts. for example "newstring" has no meaning to me. `n is a newline, ASCII character 13
I haven't posted anything about ControlGetText. if you are retrieving text from notepad that way you should post exactly what you're capturing as a long string.

file =
(

)

was just used to put some newline example data into a variable. Reading it from a file such as FileRead, OutputVar, % A_ScriptDir "\file.txt" would read the file into the "OutputVar" variable. As I said in my first post, a file written with windows notepad will most likely pair each newline with a return carriage.
\r matches a carriage return (ASCII 13)
\n matches a line-feed (newline) character (ASCII 10)

If you want to match both using regex; use \s (matches any whitespace character (equal to [\r\n\t\f\v ]))
mojobadshah
Posts: 25
Joined: 12 Nov 2019, 16:32

Re: How to translate javascript regex to AHK ?

21 Nov 2019, 17:45

Chunjee wrote:
21 Nov 2019, 16:01
I think you should just post the file you are working with and the ideal output. I'm not understanding most of your posts. for example "newstring" has no meaning to me. `n is a newline, ASCII character 13
Right now when I run this ahk....

Code: Select all

ControlGetText, Data, Edit1, Untitled - Notepad ; 
{
Data := RegExReplace(Data, "im)([<])","\n$1")  		
}
FileAppend,  %Data%, Untitled.txt

....with a Notepad file with the name "Untitled.txt" and text...


'Mary<John<Mary

Mary<Doe<Mary'


...in the designated Notepad file "Untitled.txt" in the same folder resolves to....

'
Mary<John<Mary

Mary<Doe<MaryMary\n<John\n<Mary

Mary\n<Doe\n<Mary'

...IN NOTEPAD... And, I'd like to see this resolve to:

'
Mary

<John

<Mary

Mary

<Doe

<Mary

'

OVERWRITTEN.... And in the same Notepad file 'Untitled.txt"

I work with both javascript and regex a lot, and I have a descent inclination with AHK. Had autohotkey regex translated conveniently over to AHK I think I'd be, ok, but I've been playing around with this formula mainly and there doesn't appear to be a host of options to effect the parameters in such a way where that \n behaves the same way it does in a regular [js] regex function.
User avatar
Chunjee
Posts: 1498
Joined: 18 Apr 2014, 19:05
Contact:

Re: How to translate javascript regex to AHK ?

22 Nov 2019, 09:50

Probably because ahk doesn't have the same /g mode or it's always on I think.

Code: Select all

file =
(
Mary<John<Mary

Mary<Doe<Mary
)

NewStr := RegExReplace(file, "([<])", "`n<")
NewStr := RegExReplace(NewStr, "[\s]+", "`n`n`n")
FileAppend, %NewStr%, % A_ScriptDir "\Untitled.txt"
ExitApp, 1
Seems to match what you are looking for. Replaces all "<" with a newline and "<"
Then capture any 1-* newlines and return carriages and replace them with three newlines

Image

OVERWRITTEN.... And in the same Notepad file 'Untitled.txt"
I'll let you figure that out. It sounds like you were capturing with something else than FileRead, which means the file is open? You typically can't overwrite a file that is open in another app in windows.



I think the approach is a little brittle. It's basically string replacement. I would but each element into an array then you have more control over how many newlines separate each element.

requires https://www.npmjs.com/package/biga.ahk

Code: Select all

A := new biga()

file =
(
Mary<John<Mary

Mary<Doe<Mary
)


Arr := A.split(file, "/[<\n]/") ; split on newlines and "<"
Arr := A.compact(Arr) ;remove blank line elements
; msgbox, % A.printObj(Arr)
NewStr := A.join(Arr, "`n`n`n<")
FileAppend, %NewStr%, % A_ScriptDir "\Untitled.txt"
ExitApp, 1
Image
mojobadshah
Posts: 25
Joined: 12 Nov 2019, 16:32

Re: How to translate javascript regex to AHK ?

22 Nov 2019, 13:22

Chunjee wrote:
22 Nov 2019, 09:50

I think the approach is a little brittle. It's basically string replacement.
An immense thanks to you for helping me to sort this out. This is eventually how put it all together to form a working ahk script:


ControlGetText, NewStr, Edit1, Untitled - Notepad ;

{

NewStr := RegExReplace(NewStr, "([<])", "`n<")
NewStr := RegExReplace(NewStr, "[\s]+", "`n`n`n")



}

FileAppend, %NewStr%, Untitled.txt
mojobadshah
Posts: 25
Joined: 12 Nov 2019, 16:32

Re: How to translate javascript regex to AHK ?

22 Nov 2019, 20:03

What's the standard conversion for regex '.+' for example:

Code: Select all

NewStr := RegExReplace(NewStr, ".+(anyword).+","")
AHK regex, popularly, appears to favor ' .* ' but I've also seen posts inclusive of ' .+ ' so why would using the latter ' .+ ' cause ahk to lag and not fully process?
User avatar
Chunjee
Posts: 1498
Joined: 18 Apr 2014, 19:05
Contact:

Re: How to translate javascript regex to AHK ?

22 Nov 2019, 20:20

Maybe the string being fed into the function is really really long? .+ and .* are both valid for their own use cases.

if ahk is going slower than expected you may want to specify SetBatchLines, -1 https://www.autohotkey.com/docs/commands/SetBatchLines.htm
mojobadshah
Posts: 25
Joined: 12 Nov 2019, 16:32

Re: How to translate javascript regex to AHK ?

23 Nov 2019, 17:19

Chunjee wrote:
22 Nov 2019, 20:20
Maybe the string being fed into the function is really really long? .+ and .* are both valid for their own use cases.

if ahk is going slower than expected you may want to specify SetBatchLines, -1 https://www.autohotkey.com/docs/commands/SetBatchLines.htm
It would appear that in order for js regex to convert correctly over to ahk regex '.+(anyword).+' is more effectively written as '\s.*(anyword).*\s' . In relation to moderating CPU rates SetBackLine was not going to make this work 100%, but something related to processing and sequence did become obvious to a holdback in regards to this ahk projection. Is anything parallel to a 'WinWait' operator or 'ProcessWait' operator for the sake of making the NewStr functions indicated below initiate a,b,c, etc... sequentially? I noticed someone trying to implement 'Sleep' operator after every succeeding ahk string function, but that didn't seem to work for me according to this specific case. I've provided an example of a working ahk:

Code: Select all

ControlGetText, NewStr, Edit1, Untitled - Notepad ; 

{

NewStr := RegExReplace(NewStr, "\s.*(anyword1).*\s","")

 NewStr := RegExReplace(NewStr, "\s.*(anyword2s).*\s","")
	
}

FileAppend,  %NewStr%, Untitled8.txt

Is there anything that is sure to work in terms of making ' NewStr := RegExReplace(NewStr, "\s.*(anyword1).*\s","") ' happen first and then ' NewStr := RegExReplace(NewStr, "\s.*(anyword2).*\s","") ' concretely, relative to entering a 'Sleep' operator after each NewStr expression? I would like to prevent having to do an entire one of these ControlGetText--NewStr-FileAppend ahk systems.
User avatar
Chunjee
Posts: 1498
Joined: 18 Apr 2014, 19:05
Contact:

Re: How to translate javascript regex to AHK ?

23 Nov 2019, 22:36

ahk executes sequentially. If NewStr was not modified on the first RegExReplace it is because there was no matching pattern.


I guess you could do this:

Code: Select all

String := "hello Fred!"

NewStr := RegExReplace(RegExReplace(String, "Fred", "Barney"), "\!", ".")
msgbox, % NewStr
; => hello Barney.
mojobadshah
Posts: 25
Joined: 12 Nov 2019, 16:32

Re: How to translate javascript regex to AHK ?

26 Jan 2020, 21:42

Chunjee wrote:
22 Nov 2019, 20:20
Maybe the string being fed into the function is really really long? .+ and .* are both valid for their own use cases.

if ahk is going slower than expected you may want to specify SetBatchLines, -1 https://www.autohotkey.com/docs/commands/SetBatchLines.htm
I've noticed pretty effectively that regex doesn't work with a lot of data very well. Can you provide an example of an inverse ahk regex that is maybe preceded by an ahk delete function first?
User avatar
Chunjee
Posts: 1498
Joined: 18 Apr 2014, 19:05
Contact:

Re: How to translate javascript regex to AHK ?

27 Jan 2020, 06:44

mojobadshah wrote:
26 Jan 2020, 21:42
I've noticed pretty effectively that regex doesn't work with a lot of data very well. Can you provide an example of an inverse ahk regex that is maybe preceded by an ahk delete function first?
A lot going on here. Not sure what example you are looking for and regex works with any string data.
mojobadshah
Posts: 25
Joined: 12 Nov 2019, 16:32

Re: How to translate javascript regex to AHK ?

29 Jan 2020, 13:30

Chunjee wrote:
27 Jan 2020, 06:44
mojobadshah wrote:
26 Jan 2020, 21:42
I've noticed pretty effectively that regex doesn't work with a lot of data very well. Can you provide an example of an inverse ahk regex that is maybe preceded by an ahk delete function first?
A lot going on here. Not sure what example you are looking for and regex works with any string data.
I have data in a text file like:

Fred
Barney
Fred

My ahk regex is:

ControlGetText, NewStr, Edit1, Untitled - Notepad ;

{

NewStr := RegExReplace(NewStr, "(.+Barney.+)", "$1")

}

FileDelete Untitled.txt
FileAppend, %NewStr%, Untitled.txt

I can capture a string of text with Barney in it. What convention do I need, however, to delete everything before and after (.+Barney.+) or "$1" ?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 255 guests