Read a string and perform different actions when certain subpatterns are found (must be easy but I am lost) Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Nixcalo
Posts: 116
Joined: 06 Feb 2018, 04:24

Read a string and perform different actions when certain subpatterns are found (must be easy but I am lost)

14 Jun 2020, 21:47

I have been fighting with RegexReplace, RegexMatch and InSrt and the like for hours, and I am lost and depressed.

Here are my needs.

Let's consider I have a string, such as:
String:="Atención: 3╚1>Brillo╚1>╚2>El operador puede aumentar o disminuir el brillo de la pantalla.╚2>╚3>"

I need to take that string from a certain place and to another, but I have to split it first, because the actual meaning of ╚1> is really Alt+1, the meaning of ╚2> is really Alt+2, etc (I need to enter this string in a place that allows for tags, and tags are introduced with Alt+Number). But the text comes from a place where tags are not allowed, so I have devised this way to mark them. I have chosen this ╚ because it's an ideal separator as I won't find it in normal text (that's why I could not use #, | or !). Note that the number coming after ╚ is just a shorthand for sending a Alt-<number> command; that tag number must NOT be present in the final string, and the same happens with > and .

So when I take the string to another place, the script has to do the following.
Take the entire string above and do the following with it:

Make ClipBoard:="Atención: 3"
SendInput, ^v
Perform a SendInput, !1 command.
Make ClipBoard:="Brillo"
SendInput, ^v
Perform a SendInput, !1 command.
Perform a SendInput, !2 command.
Make ClipBoard:="El operador puede aumentar o disminuir el brillo de la pantalla."
SendInput, ^v
Perform a SendInput, !2 command.
Perform a SendInput, !3 command.

End of the process
At the end of the process, the original string will be neatly introduced in a place and where ╚1>, ╚2>, ╚3> were before, now it will have some nice tags the application will have inserted in its place.

Please note that there may be any number of tags (from 1 to 10, actually) and they can be placed anywhere, in the beginning of the string, at the end, there may be several consecutive tags or none at all.

I have been fighting for hours,. I pondered about this:
1.-Whether to use StrSplit (arrays) to split the sentence, but decided against it because I needed the number right before the ╚, and if I used the ╚ as a delimiter, ╚ would be lost from the array so the numbers there could be confused with a normal non-tag number (hence I needed to have both the number and the ╚ so I could know I had a tag and not a regular number.
2.- I decided to use While StrLen(String)>0 and then go chopping the string part by part, using a mix of SubStr, and RegexReplace to get the parts I needed and then discarding then once copied to the ClipBoard. It was a cool idea but I encountered problems such as knowing what items to keep, which to discard, when to discard them and most importantly, what would the next step of the While loop be or in what position of the string I was at the time.
3.- To start with something like while nn := RegExMatch(String, "╚(\d+)>", m,nn?nn+1:1) { I saw from another post. However, I soon got lost with the m's, nn's, m1's and what the position or what the heck all of it was. My head got spining.

4.- I thought about a few seconds about For loops, but .. I don't even understand the syntaxt of it (wasn't it that hard to say For X=1 to 10 as it was in good ol' Basic????) What the heck is that For, x, y in z???
4.- Ask for help as I need to deliver a huge thing in like 10 hours, haven't slept and I need to leave the PC working with this script for a few hours so I could rest and find some of the job done.

Thank you, life-savers!
Nixcalo
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Read a string and perform different actions when certain subpatterns are found (must be easy but I am lost)

14 Jun 2020, 22:28

It would be really simple if you just used as a delimiter in all cases so you could use a simple StrSplit. Then every 1> type of segment would mean to do the SendInput thing with the number, and all others would be text to pasted from the clipboard. You’re just over complicating it by not doing that. Then your string would look like this:

String:="Atención: 3╚1>╚Brillo╚1>╚2>╚El operador puede aumentar o disminuir el brillo de la pantalla.╚2>╚3>"
Nixcalo
Posts: 116
Joined: 06 Feb 2018, 04:24

Re: Read a string and perform different actions when certain subpatterns are found (must be easy but I am lost)

14 Jun 2020, 22:58

I am sorry but it's 6 am and my mind is not what it used to be. How would it be done? I am going to try on my own as well, but I am so dense at this time of the night I could be writing Chinese for what it's worth (and I don't speak a word of Chinese)
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Read a string and perform different actions when certain subpatterns are found (must be easy but I am lost)

14 Jun 2020, 23:04

Use the string as I showed in my post, then just use StrSplit with that special character. Very straightforward.
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Read a string and perform different actions when certain subpatterns are found (must be easy but I am lost)  Topic is solved

14 Jun 2020, 23:14

I believe this is what you want:

Code: Select all

String := "Atención: 3╚1>╚Brillo╚1>╚2>╚El operador puede aumentar o disminuir el brillo de la pantalla.╚2>╚3>"
Segments := StrSplit(String, "╚")
for Each, Segment in Segments
	if RegExMatch(Segment, "\d(?=>)", Num)
		SendInput, !%Num%
	else
	{
		ClipBoard := Segment
		SendInput, ^v
	}
Nixcalo
Posts: 116
Joined: 06 Feb 2018, 04:24

Re: Read a string and perform different actions when certain subpatterns are found (must be easy but I am lost)

14 Jun 2020, 23:24

I am so tired at this moment that I am really struggling to see the difference between your string and my string and how you obtained yours from mine. I am THAT tired!

But I will check out your code. I regret somewhat that I used a bit of a "prepared" string instead of an actual one and I am now struggling to see how to apply those delimiters to my actual string. Damn! I can barely add 2+2 now. But thank you! I will thank you more properly when I see how to adapt this to my needs but it's amazing how you guys are so familiar with AHK secrets.
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Read a string and perform different actions when certain subpatterns are found (must be easy but I am lost)

14 Jun 2020, 23:30

My string has the character between each segment. So it's inserted after the 1>, 2>, etc., not just before. That way the StrSplit creates an array broken into this:
Atención: 3
1>
Brillo
1>
2>
El operador puede aumentar o disminuir el brillo de la pantalla.
2>
3>


That makes it very easy to act on each part. If it is a number followed by a >, then do a SendInput, ! followed by the number. All other parts get pasted from the clipboard.
Nixcalo
Posts: 116
Joined: 06 Feb 2018, 04:24

Re: Read a string and perform different actions when certain subpatterns are found (must be easy but I am lost)

14 Jun 2020, 23:33

I think your actual string does not work as it's not how my strings are prepared. The actual string I have is something like this (one of the many).

Atención: 3<TAG1>Brillo<TAG1><TAG2>El operador puede aumentar o disminuir el brillo de la pantalla.<TAG2><TAG3>

I performed a simple
String:=StrReplace(String,"<TAG", "╚") to get my adapted string and the result is
String:="Atención: 3╚1>Brillo╚1>╚2>El operador puede aumentar o disminuir el brillo de la pantalla.╚2>╚3>"
I think yours is not the same.
Yours is String:="Atención: 3╚1>╚Brillo╚1>╚2>╚El operador puede aumentar o disminuir el brillo de la pantalla.╚2>╚3>" and I am not very sure how you can get that from my actual string.
I am going to see (if I can keep my eyes open) if I can adapt your code to my actual string... It does not seem that hard, but I have been working for 12 hours now and I am ... kind of dead.

Edit: Ok, I have finally been able to get your string, it's pathethically bad regex but I got it this way.

Code: Select all

String:=RegexReplace(String,"<TAG", "╚")
String:=RegexReplace(String,"(\d>)", "$1╚")
String:=RegexReplace(String,"╚╚", "╚")
String:=RegexReplace(String,"╚$", "")
String:=RegexReplace(String,"^╚", "")
It may not be beautiful but I finally got your string. Let's see if the rest works as well!

EDIT: YAY IT WORKS!!! I can finally leave the computer working and go to sleeep! YAY!!!

Thank you boiler!!!!!!!!!!!!!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, mikeyww and 293 guests