RegExMatch Question: Matching Numbers Between Two Symbols Topic is solved

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

RegExMatch Question: Matching Numbers Between Two Symbols

14 Nov 2017, 16:20

Guys, I'm strugling with the following little issue. I have a string like this:

Code: Select all

&length_seconds=1081&of=
, where numbers change every time. I need to create a RegExMatch that would capture whatever numbers that are between the "&length_seconds=" and "&of=" pieces of text. Could anyone be so very kind to help me out with this please? :cry:

I tried the following

Code: Select all

FoundPos := RegExMatch(VideoLengthRaw, "(?<=&length_seconds=).*?(?&of=)", VideoLength)
, so basically

Code: Select all

"(?<=length_seconds=).*?(?=&relative_loudness)"
as the regex. But doesn't quite work :headwall:
egocarib
Posts: 100
Joined: 21 May 2015, 18:21

Re: RegExMatch Question: Matching Numbers Between Two Symbols

14 Nov 2017, 16:30

You are missing an equals sign in your regex string.

Old: (?<=&length_seconds=).*?(?&of=)
New: (?<=&length_seconds=).*?(?=&of=)

Note that \d matches only numbers. So it might be easier to use that in your regex match string, depending on what the rest of the pattern looks like. For example, the simple regex pattern \d+ matches the number in the example string you provided.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: RegExMatch Question: Matching Numbers Between Two Symbols

14 Nov 2017, 16:32

Hello. Please try this,

Code: Select all

str:="&length_seconds=1081&of="
regexmatch(str,"O)&length_seconds=(\d+)&of=",m)
msgbox % m.pos(1)  "`t" m[1]
adamas
Posts: 10
Joined: 14 Nov 2017, 16:22

Re: RegExMatch Question: Matching Numbers Between Two Symbols

14 Nov 2017, 16:51

egocarib wrote:You are missing an equals sign in your regex string.

Old: (?<=&length_seconds=).*?(?&of=)
New: (?<=&length_seconds=).*?(?=&of=)

Note that \d matches only numbers. So it might be easier to use that in your regex match string, depending on what the rest of the pattern looks like. For example, the simple regex pattern \d+ matches the number in the example string you provided.
egocarib, for some reason this solution doesn't work. Meaning, if I replace my original non-working

Code: Select all

FoundPos := RegExMatch(VideoLengthRaw, "(?<=&length_seconds=).*?(?&of=)", VideoLength)
with your suggestion

Code: Select all

FoundPos := RegExMatch(VideoLengthRaw, "(?<=&length_seconds=).*?(?=&of=)", VideoLength)
- it still doesn't seem to be working. I check this by having a

Code: Select all

MsgBox % VideoLength
after the FoundPos line - and it shows nothing when I use updated code (same as when I use mine). Or maybe I'm applying your solution incorrectly? :?

Also, any chance you could show how to apply your \d recommendation on my example? Sorry for asking, but I'm simply not that familiar with regex for now, only starting with it :)



Helgef wrote:Hello. Please try this,

Code: Select all

str:="&length_seconds=1081&of="
regexmatch(str,"O)&length_seconds=(\d+)&of=",m)
msgbox % m.pos(1)  "`t" m[1]
Helgef, is there a way to integrate your suggestion into my original

Code: Select all

FoundPos := RegExMatch(VideoLengthRaw, "(?<=&length_seconds=).*?(?&of=)", VideoLength)
string? Integrate in this context simply means "fix" my original string. I tried just taking the

Code: Select all

"O)&length_seconds=(\d+)&of="
from it, but I can't get it to work :(

The reason I need to "fix" my original string, is because it should store the numbers it takes using regex into a variable called VideoLength.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: RegExMatch Question: Matching Numbers Between Two Symbols  Topic is solved

14 Nov 2017, 17:22

Other than the obvious, I have no good suggestion,

Code: Select all

regexmatch(str,"&length_seconds=(?P<ideoLength>\d+)&of=", V) ; :(
msgbox % VideoLength
The obvious
adamas
Posts: 10
Joined: 14 Nov 2017, 16:22

Re: RegExMatch Question: Matching Numbers Between Two Symbols

14 Nov 2017, 18:14

Helgef wrote:Other than the obvious, I have no good suggestion,

Code: Select all

regexmatch(str,"&length_seconds=(?P<ideoLength>\d+)&of=", V) ; :(
msgbox % VideoLength
The obvious
Helgef, thanks so much again for your input, I really do appreciate this a ton! I'm new to this and it really does help.

Coming back to the issue, now I think the problem is with the input I use. So, my script takes this file https://www.dropbox.com/s/li5f8fursrovw ... _info?dl=0 stores it into a variable called VideoLengthRaw via a method described here https://autohotkey.com/docs/commands/UR ... ToFile.htm. And then I'm trying to do regex via my original string: FoundPos := RegExMatch(VideoLengthRaw, "(?<=&length_seconds=).*?(?&of=)", VideoLength)

Basically, your suggestion works with str:="&length_seconds=1081&of=" - and it works perfectly. But if I try to do str := VideoLengthRaw - it doesn't work at all.



Am I doing something wrong in terms of storing the file in the variable OR in terms of how I try to do regex on that variable?
egocarib
Posts: 100
Joined: 21 May 2015, 18:21

Re: RegExMatch Question: Matching Numbers Between Two Symbols

14 Nov 2017, 19:06

You need to debug a bit. What does str look like after you do str := VideoLengthRaw?

Try MsgBox % VideoLengthRaw, or if it's a large file, try Clipboard := VideoLengthRaw and then paste the output into notepad to see what the variable contents look like.
adamas
Posts: 10
Joined: 14 Nov 2017, 16:22

Re: RegExMatch Question: Matching Numbers Between Two Symbols

14 Nov 2017, 19:16

egocarib wrote:You need to debug a bit. What does str look like after you do str := VideoLengthRaw?

Try MsgBox % VideoLengthRaw, or if it's a large file, try Clipboard := VideoLengthRaw and then paste the output into notepad to see what the variable contents look like.
VideoLengthRaw is this file ( https://www.dropbox.com/s/li5f8fursrovw ... _info?dl=0 ) saved into that variable via URLDownloadToFile function (whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")). I tried debugging via both the methods you suggested, and the contents of VideoLengthRaw looks exactly like whole file - check out that dropbox link which has that whole text file.

So, the file saved in the variable looks OK I think (but maybe I'm wrong), it has the "&length_seconds=1081&of=" part from which I need to extract 1081 via regex - meaning numbers between "&length_seconds=" and "&of=". So my dilemma is still - which is the regex that would work on the VideoLengthRaw variable? :?
egocarib
Posts: 100
Joined: 21 May 2015, 18:21

Re: RegExMatch Question: Matching Numbers Between Two Symbols

14 Nov 2017, 20:50

Post your full script, and we might be able to get to the bottom of the issue. I downloaded the file and the regex Helgef or I provided looks like it should work fine.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: scriptor2016 and 291 guests