StrSplit not splitting? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 08 Jan 2022, 17:21

Okay, a little update as I am hitting a bump in the road.
Some text may sometimes be added to my clipboard string. So I need to use RegExMatch as you suggested. But I been trying for two days to understand how that works. I give up for now. I am reading a book I ordered on Amazon about RegEx. Maybe that will help. In the mean time I will have to use your suggested code, boiler...without really understanding it for now.

But something would really help me if it's possible. In the following MsgBox, you see how there are some substrings that come out empty.
Image
Please note that the "()" was placed after the substring so I could see if there was any spaces.
Is there a way to produce an array with StrSplit that can omit the empty substrings so that "pending" comes out in 1# substring and "Contract" comes out in 2# substring?

Because if I use your code to find "pending" between the two empty substrings, that's going to cause some problems.

Thanx in advance for your great help!

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: StrSplit not splitting?

Post by boiler » 08 Jan 2022, 18:33

That output is not the output from the code I showed using RegExMatch, so I guess you're saying you're reverting back to the StrSplit() approach. Then what you're showing there are not "substrings" as you called them. They are array elements. Here's how you can get rid of empty array elements:

Code: Select all

; create the array:
Haystack =
(

New contract to sell 0.00179517 BTC with PepeLapiu2


Please check your notification page for details


)
Array := StrSplit(Haystack, [A_Space, "`n", "`r"])

; display the array before removing any elements:
DisplayArray(Array)

; remove the empty array elements (starting from the end, working backwards):
c := Array.Count()
loop, % c {
	i := c - A_Index + 1
	if (Array[i] = "")
		Array.RemoveAt(i)
}

; display the array after removing empty elements:
DisplayArray(Array)
return

; function used for displaying the array elements in a MsgBox:
DisplayArray(a) {
	for i, v in a
		out .= v "`n"
	out := SubStr(out, 1, -1) ; remove the extra `n on the end
	MsgBox, % "---TOP---`n" out "`n---BOTTOM---"
}
It starts from the end and works backwards because if you remove the from the beginning and go forward, it messes up the index because the remaining element shift up one.

You don't have to worry about there being unseen spaces in there because they would have been removed by the StrSplit().

PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 08 Jan 2022, 19:28

boiler wrote:
03 Jan 2022, 10:36
This will only extract the data if the number is between "sell" and "BTC" and is followed by "with" and a name:

Code: Select all

HayStack =
(
 	
New contract to sell 0.00341901 BTC with PepeLapiu2

Please check your notifications page for details


)
RegExMatch(Haystack, "sell ([\d\.]+) BTC with (\w+)", m)
MsgBox, % "Qty of BTC: " m1 "`nUser name: " m2

Okay. So here is what I was hoping would work, but it doesn't. It only produces an empty variable.

First here is my clipboard:

pending
Contract to sell 0.018045 BTC for 900 CAD

Your counterparty: LINCOLNPOLLAR

Link to the offer
Price: 50000 CAD
You are selling: 0.018045 BTC
You receive: 900 CAD
Location: Canada
Depositing window:
30 min.
Buyer can cancel the contract once the depositing window is expired
Payment window:
90 min.
Counter starts when deposit is confirmed
Confirmations (BTC) required: 1
Payment method

Online payment system - Interac e-transfer
And here is the script I tried with fail:

Code: Select all

Clipboard1 := % "start" Trim(Clipboard, [A_Space, "`r"]) " stop"   ;adding start and stop to begining and end of script.
MsgBox, % Clipboard1
RegExMatch(Clipboard1, "start ([\d\.]+) Contract (\w+)", m)
MsgBox, % "Contract state: " m1
I was hoping it would produce a "pending" with spaces and returns on each side of it and I would clean it up with Trim function.

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: StrSplit not splitting?

Post by boiler » 08 Jan 2022, 21:16

If you’re going to use RegExMatch, don’t first try to split the string (at least that looks like what you were trying to do) and then have it produce some intermediate output that you then parse and/or clean up with other methods. Just use RegExMatch by itself to give you the desired output. What is your desired output from that text? Do you really just want to extract the word “pending” regardless of whether there are spaces and returns on each side of it? Why?

Also, what is the following line supposed to be?

Code: Select all

Clipboard1 := % "start" Trim(Clipboard, [A_Space, "`r"]) " stop"
There are several things wrong with it that show you’re not really absorbing anything we’re posting, and you’re not trying to follow the syntax of the various commands/functions as shown in the documentation.
  • Again (and again and again…), the := operator is for expression assignment. There is no reason to follow it with a % to force an expression.
  • That is not the parameter list for Trim(). You are mixing up StrSplit() and Trim().
  • If you’re going to try to split by linefeeds, you need to at least include `n. Lines are often broken by `n or both `n and `r, but very rarely `r alone.

PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 08 Jan 2022, 22:37

boiler wrote:
08 Jan 2022, 21:16
If you’re going to use RegExMatch, don’t first try to split the string (at least that looks like what you were trying to do) and then have it produce some intermediate output that you then parse and/or clean up with other methods.
That is what I tried to do at first, At first I took your

Code: Select all

RegExMatch(Haystack, "sell ([\d\.]+) BTC with (\w+)", m)
MsgBox, % "Qty of BTC: " m1 "`nUser name: " m2
and I changed it to this in order to extract the "pending" into a Var1.
Here is my first try:

Code: Select all

Clipboard content: "


pending
Contract to buy 0.01822727 BTC for 1000 CAD

Your counterparty: PepeProton

Link to the offer
Price: 	55000 CAD
You are buying: 	0.01822727 BTC
You must send: 	1000 CAD
Location: 	Canada
Depositing window: 	
30 min.
You can cancel the contract once the depositing window is expired
Payment window: 	
90 min.
Counter starts when deposit is confirmed
Confirmations (BTC) required: 	1
Buyer's release address in this contract: 	bc1q4ykakefv4uv6k9682k7x9ykyhun46zf5ga5wgz
Payment method 	

Online payment system - Interac e-transfer


"

RegExMatch(Clipboard, "A_Space ([\d\.]+) Contract (\w+)", m)
MsgBox, % "Contract state: " m1

That didn't work, turned out an empty string.
Than I though I should add a word at the beginning and at the end of the string. So maybe I could catch "pending" between "start" and "Contract" like this:

Code: Select all

;Clipboard = same as above try.
Clipboard1 = % "start" Clipboard " stop"
RegExMatch(Clipboard, "A_Space ([\d\.]+) Contract (\w+)", m)
MsgBox, % "Contract state: " m1
Again, nothing but an empty string resulted.
I was guessing it can't find it because there are like 4 or 5 spaces at the beginning of the clipboard string. And there is also a new line and/or return between "pending" and "Contract". So I tried to remove the spaces and line breaks and returns from both ends of my clipboard, like this: Clipboard1 = % "start" Trim(Clipboard, [A_Space, "`r"]) " stop". And into this:

Code: Select all

;Clipboard = same as above try.
Clipboard1 = % "start" Trim(Clipboard, [A_Space, "`r"]) " stop"
RegExMatch(Clipboard, "A_Space ([\d\.]+) Contract (\w+)", m)
MsgBox, % "Contract state: " m1
Basically, your code works well when the word I want is in a phrase between two other words on the same line. Like this:

Code: Select all

HayStack =
(
 	
New contract to sell 0.00341901 BTC with PepeLapiu2

)
RegExMatch(Haystack, "([\d\.]+) BTC with (\w+)", m)
MsgBox, % "Qty of BTC: " m1 "
But I can't make it work if the word I want is at the beginning or end of a line, with a space/line break/return before or after the word I want.

You get what I am trying to do here?

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: StrSplit not splitting?

Post by boiler » 08 Jan 2022, 22:49

No, I still don’t get what you want. You want to match the word “pending”? In relation to what specifically around it? You just want to know if the text contains the word “pending”?

Also, you are not even trying to understand what the RegEx pattern does. You are just sticking random stuff into a pattern of RegEx symbols that had a very specific purpose like matching digits. Don’t just make wild guesses at stuff.

And you’re still not understanding the fundamentals of expressions when you put A_Space in the middle of a quoted string. I ask you once again to study, study, study the stuff about expressions until you understand it and can routinely write simple scripts with correct expressions in them. Otherwise, there’s no point to us answering your posts because they don’t lead to you producing better code by your next post. There has to be some progression being made or we’re just wasting time.

PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 08 Jan 2022, 22:59

boiler wrote:
08 Jan 2022, 22:49
No, I still don’t get what you want. You want to match the word “pending”? In relation to what specifically around it? You just want to know if the text contains the word “pending”?
The word "pending" represents the status of a contract. It could be "cancelled" or "waiting" or "disputed" or "marked" or a few other words depending where in the contract we are at the moment. It's always the first word of the clipboard. But on it's own line by itseld. I want to fetch that word so the script can update the progress of the contract and respond accordingly.

I know I am not doing it right dude. If I were doing it right, I'd be the one showing you how to do it.

I am currently reading a book on RegEx. So I'm trying.

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: StrSplit not splitting?

Post by boiler » 08 Jan 2022, 23:13

If you just want to see if it contains the word "pending", you're making it way too hard. Just use InStr() to see if it's in there:

Code: Select all

Clipboard =
(


pending
Contract to buy 0.01822727 BTC for 1000 CAD

Your counterparty: PepeProton

Link to the offer
Price: 	55000 CAD
You are buying: 	0.01822727 BTC
You must send: 	1000 CAD
Location: 	Canada
Depositing window: 	
30 min.
You can cancel the contract once the depositing window is expired
Payment window: 	
90 min.
Counter starts when deposit is confirmed
Confirmations (BTC) required: 	1
Buyer's release address in this contract: 	bc1q4ykakefv4uv6k9682k7x9ykyhun46zf5ga5wgz
Payment method 	

Online payment system - Interac e-transfer


)

HasPending := InStr(Clipboard, "pending")
MsgBox, % "The clipboard " (HasPending ? "contains" : "does not contain") " the word 'pending'."

I am not expecting you to understand RegEx pattern creation yet. I am saying you need to spend your time on understanding how to write expressions in AHK before you move on to anything else.

PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 08 Jan 2022, 23:14

What I trying to do here is this.
The bitcoin market is saturated with traders who will sell you or buy from you large sums of Bitcoin on peer-to-peer exchanges. But they usually won't waiste their time with a trade under $2000 because it waists too much of their time for too little a profit.

So they will stay away from trades under $2000 or they will jack up their fees to the point nobody wants to buy from them.

But if I can automate my traders I can buy and sell with my clients for dirt cheap with trades as low as $50 and still make a good profit without charging them an arm and a leg.

So my script watches my email for an alert a new buyer or seller is interested in my offer. Than it logs on to the exchange, and performs the trade for me.

- Explains the terms to the client.
- Asks him to agree to the terms of the contract.
- Sends the bitcoin into the multisig address.
- Provide buyer with information on where to send the fiat payment.
- Returns to email for a notification from my bank that the payment has been received.
- Logs back on the exchange to release the coin from the multisig address to the buyer.

Done.
Last edited by PepeLapiu on 09 Jan 2022, 00:46, edited 1 time in total.

PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 08 Jan 2022, 23:31

boiler wrote:
08 Jan 2022, 23:13
If you just want to see if it contains the word "pending", you're making it way too hard. Just use InStr() to see if it's in there:

Code: Select all

Clipboard =
(


pending
Contract to buy 0.01822727 BTC for 1000 CAD

Your counterparty: PepeProton

Link to the offer
Price: 	55000 CAD
You are buying: 	0.01822727 BTC
You must send: 	1000 CAD
Location: 	Canada
Depositing window: 	
30 min.
You can cancel the contract once the depositing window is expired
Payment window: 	
90 min.
Counter starts when deposit is confirmed
Confirmations (BTC) required: 	1
Buyer's release address in this contract: 	bc1q4ykakefv4uv6k9682k7x9ykyhun46zf5ga5wgz
Payment method 	

Online payment system - Interac e-transfer


)

HasPending := InStr(Clipboard, "pending")
MsgBox, % "The clipboard " (HasPending ? "contains" : "does not contain") " the word 'pending'."

I am not expecting you to understand RegEx pattern creation yet. I am saying you need to spend your time on understanding how to write expressions in AHK before you move on to anything else.
I'm not trying to look for the word "pending" specifically. It is always the first word of the clipboard, But with line breaks/spaces/returns before it.

The status of the trade could be "disputed", "cancelled", "pending" or "waiting" or "paid" or "completed". But it's always the first word of my clipboard string.

The danger in just looking for the word "pending" or "paid" anywhere in the string is that some user with the word "paid" or "paid user name" could fool the script into thinking the contract has been paid when it hasn't.

I guess I'll have to read that book now.

And don't kid yourself about syntax. I'm doing better man. Today I only saw very little smoke coming out of my computer. I'm getting better. But I still suck at it thought.

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: StrSplit not splitting?

Post by boiler » 08 Jan 2022, 23:38

PepeLapiu wrote: I'm not trying to look for the word "pending" specifically. It is always the first word of the clipboard, But with line breaks/spaces/returns before it.

The status of the trade could be "disputed", "cancelled", "pending" or "waiting" or "paid" or "completed". But it's always the first word of my clipboard string.

The danger in just looking for the word "pending" or "paid" anywhere in the string is that some user with the word "paid" or "paid user name" could fool the script into thinking the contract has been paid when it hasn't.
OK. This use of RegExMatch() will match only when that first word (no matter how many blank lines or spaces are before it) is "pending" and not if it appears later:

Code: Select all

Clipboard =
(


pending
Contract to buy 0.01822727 BTC for 1000 CAD

Your counterparty: PepeProton

Link to the offer
Price: 	55000 CAD
You are buying: 	0.01822727 BTC
You must send: 	1000 CAD
Location: 	Canada
Depositing window: 	
30 min.
You can cancel the contract once the depositing window is expired
Payment window: 	
90 min.
Counter starts when deposit is confirmed
Confirmations (BTC) required: 	1
Buyer's release address in this contract: 	bc1q4ykakefv4uv6k9682k7x9ykyhun46zf5ga5wgz
Payment method 	

Online payment system - Interac e-transfer


)

StartsWithPending := RegExMatch(Clipboard, "^\W*pending\b")
MsgBox, % "The clipboard " (StartsWithPending ? "starts" : "does not start") " with the word 'pending'."
PepeLapiu wrote: And don't kid yourself about syntax. I'm doing better man. Today I only saw very little smoke coming out of my computer. I'm getting better. But I still suck at it thought.
Good to hear that. Keep at it!

PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 08 Jan 2022, 23:53

boiler wrote:
08 Jan 2022, 23:13
If you just want to see if it contains the word "pending", you're making it way too hard. Just use InStr() to see if it's in there:
Thank you!!! :superhappy:

PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 09 Jan 2022, 00:01

With that code you just wrote, and your earlier RegExMatch code, I should have most of it covered.
But I know there will be a couple more places where I might have some trouble to get the data from the page.
I hope I can count on you dude.

Now off to start that book.

I would rather be able to write it myself and understand it myself, should some problem arise in the future.

PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 09 Jan 2022, 00:43

boiler wrote:
08 Jan 2022, 23:38

Code: Select all

StartsWithPending := RegExMatch(Clipboard, "^\W*pending\b")
MsgBox, % "The clipboard " (StartsWithPending ? "starts" : "does not start") " with the word 'pending'."
So how would I write that into an If statement? As in:

Code: Select all

StartsWithPending := RegExMatch(Clipboard, "^\W*pending\b")
If [pending is the first word???]
 	MsgBox, It's pending

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: StrSplit not splitting?

Post by boiler » 09 Jan 2022, 03:12

It would simply be:

Code: Select all

StartsWithPending := RegExMatch(Clipboard, "^\W*pending\b")
If StartsWithPending
 	MsgBox, It's pending
The variable isn’t even needed:

Code: Select all

If RegExMatch(Clipboard, "^\W*pending\b")
 	MsgBox, It's pending

The reason for both of the above working is StartsWithPending (or the version without it) contains the result of the RegExMatch(), which is the position of the matched string. It is zero if it was not found. And zero is the equivalent of false, and any positive number evaluates as true.

PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 09 Jan 2022, 11:24

boiler wrote:
09 Jan 2022, 03:12
The reason for both of the above working is StartsWithPending (or the version without it) contains the result of the RegExMatch(), which is the position of the matched string. It is zero if it was not found. And zero is the equivalent of false, and any positive number evaluates as true.
Lol
I don't understand a thing. RegEx is really confusing.
But thank you for the code.
:superhappy:

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: StrSplit not splitting?

Post by boiler » 09 Jan 2022, 11:29

PepeLapiu wrote: I don't understand a thing. RegEx is really confusing.
It can be one of the more confusing things to understand by just looking at someone else’s pattern and to try to decipher it. I recommend either start by building your own simple patterns, or copy existing ones into regex101.com, which will show you what each token means, either by hovering over it or by looking at the breakdown to the right.

PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 09 Jan 2022, 16:14

boiler wrote:
09 Jan 2022, 11:29
PepeLapiu wrote: I don't understand a thing. RegEx is really confusing.
It can be one of the more confusing things to understand by just looking at someone else’s pattern and to try to decipher it. I recommend either start by building your own simple patterns, or copy existing ones into regex101.com, which will show you what each token means, either by hovering over it or by looking at the breakdown to the right.
Okay, what if I want the last word of the string? That one is always the payment currency of the contract. That could be PayPal, e-Transfer, Monero, MoneyGram, Wire, or a few others as I grow the business.

Code: Select all

If (last word is e-transfer)
	MsgBox, e-transfer is the payment method

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: StrSplit not splitting?

Post by boiler » 09 Jan 2022, 16:38

Code: Select all

Clipboard =
(


pending
Contract to buy 0.01822727 BTC for 1000 CAD

Your counterparty: PepeProton

Link to the offer
Price: 	55000 CAD
You are buying: 	0.01822727 BTC
You must send: 	1000 CAD
Location: 	Canada
Depositing window: 	
30 min.
You can cancel the contract once the depositing window is expired
Payment window: 	
90 min.
Counter starts when deposit is confirmed
Confirmations (BTC) required: 	1
Buyer's release address in this contract: 	bc1q4ykakefv4uv6k9682k7x9ykyhun46zf5ga5wgz
Payment method 	

Online payment system - Interac e-transfer


)

RegExMatch(Clipboard, "\b\S+(?=\s*$)", LastWord)
if (LastWord = "e-transfer")
	MsgBox, e-transfer is the payment method

PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 17 Jan 2022, 00:30

boiler wrote:
09 Jan 2022, 16:38
First and foremost, I want to thank you a million for the help you have provided so far. I don't think you realize how much you have really contributed to my script so far. So a REALLY BIG THANK YOU.

Now I got the most important RegEx part of my script, This is where I got to copy part of the page into the clipboard and extract a Bitcoin address from it. Now this is very important. If I screw this one up, I could end up sending the coin to the wrong address and lose a large sum of money in the process. I could probably write the RegEx code for it myself as I know just enough to be dangerous. But I would really appreciate if you could look at it and give it a try. I think I would sleep better if I knew the code was not written by someone like me.
So here is the content of the clipboard:

Send exactly
0.00301504 BTC

to the address below:
3LMNaxe31YEH4k72F2KoHz6Bh34dqJkJ29

This address is a multisig escrow address. The site itself doesn't hold your money, they are locked in escrow. To release the funds, both you and the site have to sign the release transaction. After the contract's status is changed to paid, you will be signing the release transaction with your payment password, which only you know.
The important part of that haystack is the long string right below the line with "to the address below:" This is a Bitcoin receiving address, or where I send the coin into to be sold to my client. The clipboard content could at times contain more stuff above and below. But the important Bitcoin multisig address will always be right after the 'to the adress below:" line.

Can you please do that? :D

Post Reply

Return to “Ask for Help (v1)”