Why is keyword3 missing from URL? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Why is keyword3 missing from URL?

18 Sep 2021, 02:38

as per code below:

Code: Select all

::cass:: ;Search 
InputBox, Keywords, Search Keyword(s), Enter keyword(s).  Separate multiple keywords with spaces.,, 390, 135
if ErrorLevel  ; The user pressed Cancel.
    return
StringUpper Keywords, Keywords
Keywords := StrReplace(Keywords, A_Space, "+") ; replace spaces with '+'
RegExMatch(Keywords, "(\w+)\+(\w+)", M)
keyword1 := M1
keyword2 := M2
keyword3 := M3
URL := "https://www.abc.com/search/" . keyword1 . "?addRecent=false&canChangeKeyword=false&includeSuggestions=false&price_end=" . keyword3 . "&price_start=" . keyword2
msgbox, %URL%
;Run, %URL%
WinWaitActive, ahk_exe chrome.exe
return
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Why is keyword3 missing from URL?

18 Sep 2021, 03:55

the regex defines only 2 capturing groups, so M3 is a blank variable(and always will be)
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Why is keyword3 missing from URL?

19 Sep 2021, 23:00

ok got it and fixed.

how do I modify it such that it will still proceed if only 1 or 2 keywords are given (instead of 3)? currently if I do that, the output does not fill in any keyword1/2/3.
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Why is keyword3 missing from URL?

19 Sep 2021, 23:07

Use ? in the RegEx pattern to make things optional (i.e., match 0 or 1 of), like this:

Code: Select all

InputBox, Keywords, Search Keyword(s), Enter keyword(s).  Separate multiple keywords with spaces.,, 390, 135
if ErrorLevel  ; The user pressed Cancel.
    return
StringUpper Keywords, Keywords
Keywords := StrReplace(Keywords, A_Space, "+") ; replace spaces with '+'
RegExMatch(Keywords, "(\w+)\+?(\w+)?\+?(\w+)?", keyword)
MsgBox, % "keyword1: " keyword1 "`nkeyword2: " keyword2 "`nkeyword3: " keyword3

Notice that the part where it assigned M1, M2, M3 to keyword1, keyword2, keyword3 isn't needed by using keyword as the match variable instead of M.
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Why is keyword3 missing from URL?

20 Sep 2021, 06:29

I see. thank you so much!
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Why is keyword3 missing from URL?

24 Sep 2021, 19:51

boiler wrote:
19 Sep 2021, 23:07
Use ? in the RegEx pattern to make things optional (i.e., match 0 or 1 of), like this:

Code: Select all

InputBox, Keywords, Search Keyword(s), Enter keyword(s).  Separate multiple keywords with spaces.,, 390, 135
if ErrorLevel  ; The user pressed Cancel.
    return
StringUpper Keywords, Keywords
Keywords := StrReplace(Keywords, A_Space, "+") ; replace spaces with '+'
RegExMatch(Keywords, "(\w+)\+?(\w+)?\+?(\w+)?", keyword)
MsgBox, % "keyword1: " keyword1 "`nkeyword2: " keyword2 "`nkeyword3: " keyword3

Notice that the part where it assigned M1, M2, M3 to keyword1, keyword2, keyword3 isn't needed by using keyword as the match variable instead of M.
Hi there,

I got another question. If keyword1 can comprise anywhere between one to three words, while keyword2 and keyword3 are pure numbers, how can I change the regex to reflect that? for now I'm just using the + sign in input box to prevent regex from separating my keyword1, which is not the most ideal..

Separately, I also noticed keyword1 gets separated if it contains a number, E.g.

Code: Select all

test2.0
the same problem also occurs if keyword1 is a Chinese word, E.g.

Code: Select all

一样


how do I treat it as a single keyword in this case?
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Why is keyword3 missing from URL?

24 Sep 2021, 20:11

milkygirl90 wrote:
24 Sep 2021, 19:51
If keyword1 can comprise anywhere between one to three words, while keyword2 and keyword3 are pure numbers, how can I change the regex to reflect that? for now I'm just using the + sign in input box to prevent regex from separating my keyword1, which is not the most ideal..
What differentiates between the case where you have three separate keywords and case where the first keyword is made up of up to three words? Will the latter case always and only be when it is followed by pure numbers? If so, then have have one RegEx that matches one two three words followed by two numbers, and if the result of that is no match, then (and only then) execute the normal version.

milkygirl90 wrote:
24 Sep 2021, 19:51
Separately, I also noticed keyword1 gets separated if it contains a number, E.g.

Code: Select all

test2.0
the same problem also occurs if keyword1 is a Chinese word, E.g.

Code: Select all

一样


how do I treat it as a single keyword in this case?
Instead of \w to match “word” characters, use \S to match non-space characters.
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Why is keyword3 missing from URL?

24 Sep 2021, 20:48

boiler wrote:
24 Sep 2021, 20:11
Will the latter case always and only be when it is followed by pure numbers? If so, then have have one RegEx that matches one two three words followed by two numbers, and if the result of that is no match, then (and only then) execute the normal version.

Instead of \w to match “word” characters, use \S to match non-space characters.

yes.

keyword1 = up to 3 words
keyword2 = pure numbers from 0 to 1000
keyword3 = pure numbers from 0 to 1000

other than matching non-space for keyword1, it also needs to accept spaces too, E.g.

Code: Select all

bean bag

Code: Select all

hifi speakers 5.1
how do I put this into regex format? 😕

When I used /S, all the inputs went to keyword1 only and none to keyword 2 and 3.

e.g

Code: Select all

一样 10 100
keyword1: 一样 10 100
keyword 2 and 3 are blank
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Why is keyword3 missing from URL?

24 Sep 2021, 20:55

I forgot that you replaced spaces with + signs. So either remove the line where you replace spaces with + signs, or use [^+] instead of \S.
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Why is keyword3 missing from URL?

24 Sep 2021, 21:32

I updated the regex to:

Code: Select all

RegExMatch(Keywords, "([^+]+)\+?(\w+)?\+?(\w+)?", M) ;
so it works if the words are together like
一样2.0

but if it's

bean bags pro 20 100

only bean gets captured in this regex for keyword1? 🤔
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Why is keyword3 missing from URL?

24 Sep 2021, 21:51

milkygirl90 wrote:
24 Sep 2021, 21:32
I updated the regex to:

Code: Select all

RegExMatch(Keywords, "([^+]+)\+?(\w+)?\+?(\w+)?", M) ;
so it works if the words are together like
一样2.0

but if it's

bean bags pro 20 100

only bean gets captured in this regex for keyword1? 🤔
That would be the "normal" approach that would only get executed if it gets past the first RegEx that would check for two pure number on the end, in which case it would assign the rest to the first keyword, as I described before:
boiler wrote:
24 Sep 2021, 20:11
If so, then have have one RegEx that matches one two three words followed by two numbers, and if the result of that is no match, then (and only then) execute the normal version.
(poor sentence -- should have said "...that matches one, two, or three words...")


I guess you want the code instead of a description of how to do it:

Code: Select all

InputBox, Keywords, Search Keyword(s), Enter keyword(s).  Separate multiple keywords with spaces.,, 390, 135
if ErrorLevel  ; The user pressed Cancel.
    return
StringUpper Keywords, Keywords
Keywords := StrReplace(Keywords, A_Space, "+") ; replace spaces with '+'
if !RegExMatch(Keywords, "(.*)\+(\d+)\+(\d+)", keyword)
	RegExMatch(Keywords, "([^+]+)\+?(\w+)?\+?(\w+)?", keyword)
MsgBox, % "keyword1: " keyword1 "`nkeyword2: " keyword2 "`nkeyword3: " keyword3
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Why is keyword3 missing from URL?

24 Sep 2021, 22:10

I think both the code and description will help me learn faster too.

Code: Select all

if !RegExMatch(Keywords, "(.*)\+(\d+)\+(\d+)", M) 
trying to understand how this part restricts the first keyword to 3 words max (looks like a wildcard that allows any number of words?)

the other issue with this approach is that if I leave keyword2 blank, keyword3 does not get accepted unlike the original code.
E.g.
bean bags pro 200

aka the double space rightfully should allow keyword3 to be filled as 200, and keyword2 is blank.
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Why is keyword3 missing from URL?

25 Sep 2021, 04:32

milkygirl90 wrote: trying to understand how this part restricts the first keyword to 3 words max
It doesn’t. It sounded like the input format was going to be one to three words followed by two pure numbers:
milkygirl90 wrote:keyword1 can comprise anywhere between one to three words, while keyword2 and keyword3 are pure numbers
Not sure why you would enter more than three words when you are aware of the format and have to comply with it for the number part, but you could make the RegEx limit it to three words, or you could add a line afterwards that strips off anything past the third word.

milkygirl90 wrote: the other issue with this approach is that if I leave keyword2 blank, keyword3 does not get accepted unlike the original code.
E.g.
bean bags pro 200
That doesn’t meet the criteria as you described them. You said it would be followed by two pure numbers, and your example only has one. So how is it supposed to now know this is supposed to be interpreted as this format instead of the normal one when you said the distinguishing aspect would be it would end with two pure numbers?
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Why is keyword3 missing from URL?

26 Sep 2021, 02:43

boiler wrote:
25 Sep 2021, 04:32
That doesn’t meet the criteria as you described them. You said it would be followed by two pure numbers, and your example only has one. So how is it supposed to now know this is supposed to be interpreted as this format instead of the normal one when you said the distinguishing aspect would be it would end with two pure numbers?
Apologies for missing out this criteria.

In my original code, If I have 2 spaces, it will simply skip keyword2 and fill keyword 1 and 3 only.

In the updated code, I don't have this flexibility to end with either 1 or 2 pure numbers..
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Why is keyword3 missing from URL?

26 Sep 2021, 06:29

What I’m saying is that the script needs to know when to interpret the string with this new scenario where keyword1 could be made up of multiple words (so it doesn’t apply the “normal” pattern), and it seemed that the thing that signified that was that it ended in two pure numbers as the last two inputs. So are you now saying as long as the last two inputs are either a pure number or blank, then it fits this new criteria? Can both be blank?

And also, keyword1 can be from one to three words, but the user might try to enter more and the code is to just silently reject any more than three, correct?

Do you want to show an attempt at modifying the code to do this?

One important step towards learning how to code these kinds of things is to think through how to thoroughly describe the requirement to someone else. The next is, of course, to attempt to write the code yourself, which forces you to think through the logic, even if you get stuck while attempting it.
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: Why is keyword3 missing from URL?

26 Sep 2021, 08:51

boiler wrote:
26 Sep 2021, 06:29
What I’m saying is that the script needs to know when to interpret the string with this new scenario where keyword1 could be made up of multiple words (so it doesn’t apply the “normal” pattern), and it seemed that the thing that signified that was that it ended in two pure numbers as the last two inputs. So are you now saying as long as the last two inputs are either a pure number or blank, then it fits this new criteria? Can both be blank?
correct.
keyword1 comprises 1 to 3 words
keyword2 and 3 can be both blank or 1 or 2 pure numbers

And also, keyword1 can be from one to three words, but the user might try to enter more and the code is to just silently reject any more than three, correct?
yes.
Do you want to show an attempt at modifying the code to do this?
I'm having a tough time understanding what each of the symbols does in the aHK docs. It's like greek to me. For instance,

Code: Select all

Circumflex (^) matches immediately after all internal newlines -- as well as at the start of Haystack where it always matches (but it does not match after a newline at the very end of Haystack).
does not allow me to understand what this segment means

Code: Select all

([^+]+)
or if I should be reading it as 1 segment like this

Code: Select all

([^+]+)\+?
I know plus sign matches the preceding character, but there are many + signs here which baffles me, why the need? In addition, the backslash treats the + as literal. I don't understand why it needs to be treated literally since there's no + sign in my input box - just words and numbers.

It will be super tough to attempt to write the regex without first completely understanding what the code above does..
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Why is keyword3 missing from URL?  Topic is solved

26 Sep 2021, 09:15

milkygirl90 wrote: correct.
keyword1 comprises 1 to 3 words
keyword2 and 3 can be both blank or 1 or 2 pure numbers
So I guess you're saying the "normal" scenario where each keyword is a single word only would never end with two blank inputs. Is that correct?


milkygirl90 wrote: I'm having a tough time understanding what each of the symbols does in the aHK docs. It's like greek to me. For instance,

Code: Select all

Circumflex (^) matches immediately after all internal newlines -- as well as at the start of Haystack where it always matches (but it does not match after a newline at the very end of Haystack).
That is not the correct context for the ^ as used here. You are referring to the part where it is an anchor to the beginning of the haystack, or in the case you quoted, the beginning of a line when the m option is used, which was not done here. This usage is described a bit below that in Commonly Used Symbols and Syntax:
RegEx Quick Reference wrote:[^...] -- Matches any single character that is not in the class. For example, [^/]* matches zero or more occurrences of any character that is not a forward-slash, such as http://. Similarly, [^0-9xyz] matches any single character that isn't a digit and isn't the letter x, y, or z.

milkygirl90 wrote: I know plus sign matches the preceding character, but there are many + signs here which baffles me, why the need?
It is a quantifier that says to match one or more of the preceding character. If you didn't have that, then \d would only match one digit, not potentially several in a row like \d+ does. Without the +, when attempting to match 123, it would only match 1.


milkygirl90 wrote: In addition, the backslash treats the + as literal. I don't understand why it needs to be treated literally since there's no + sign in my input box - just words and numbers.
You must have forgotten that you have a line in your code that replaces all spaces with + signs (below). That's why the pattern has to match the + signs instead of space characters.

Code: Select all

Keywords := StrReplace(Keywords, A_Space, "+") ; replace spaces with '+'

I think trying to work through it yourself and asking these kinds of specific questions will help you learn these topics better than not having attempted it, and in the long run it will be time well spent.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: catquas, mikeyww, mmflume, Ralf_Reddings200244, sofista, thelegend and 127 guests