Regex on Arrays

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
NickD
Pro Scripter
Posts: 58
Joined: Fri Sep 23, 2016 2:17 pm

Regex on Arrays

Post by NickD » Fri Feb 09, 2018 3:55 pm

I am struggling to figure out how to use regex on arrays, the following script only successfully parses the first title, and then returns empty variables after that. There are 30 lines in both of the GetTag arrays.

If anybody could point me in the right direction it would be greatly appreciated.

Code: Select all

  // Titles
  IEGetTagsByAttrib>domain.com/,H3,ID=title,T,title

  // Video Links
  IEGetTagsByAttrib>domain.com,A,classname=youtube,O,url

  
  Let>k=1
  While>k<%title_Count%
  // Filter Title Array
  let>titlestring=title_%k%
  Let>pattern=.+?(?= \|)
  RegEx>pattern,titlestring,0,titlematches,num,0


  // Filter URL Array
  let>urlstring=url_%k%
  Let>pattern=^(https?\:\/\/)?(www\.youtube\.com|youtu\.?be)\/.+$
  RegEx>pattern,urlstring,0,urlmatches,num,0
  
  
  Let>title=titlematches_%k%
  Let>url=urlmatches_%k%
  Writeln>C:\Users\Shaolin Nick\Desktop\results.txt,res,%title%;%url%
  Let>k=k+1
  EndWhile
  

  GoSub>CloseIE


User avatar
Grovkillen
Automation Wizard
Posts: 1023
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Regex on Arrays

Post by Grovkillen » Fri Feb 09, 2018 5:24 pm

I sometimes concat the array into one string. Then I do the RegEx on that string.
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Regex on Arrays

Post by Marcus Tettmar » Mon Feb 12, 2018 9:47 am

I don't really follow why this isn't working. If your loop is working such that you have a different value for titlestring etc in each iteration then there's absolutely no reason why it wouldn't work.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

NickD
Pro Scripter
Posts: 58
Joined: Fri Sep 23, 2016 2:17 pm

Re: Regex on Arrays

Post by NickD » Tue Feb 13, 2018 6:05 pm

It's got me miffed too Marcus...
When I loop through the array output by IEGetTags, ArrayVar_Count returns 30 for both titles and URLs.
All the titles and URLs are unique, they are pulled from 30 different divs on the same page, each containing a different video.

Could it be something to do with the way that the IEGetTagsByAttrib> arrays are structured? I know you can't use ARC> on them so was wondering if there were other limitations?

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Regex on Arrays

Post by Marcus Tettmar » Tue Feb 13, 2018 6:34 pm

I've looped through arrays returned by IEGetTags... thousands of times.

Open IE, go to mjtnet.com and run this code:

Code: Select all

IEGetTagsByAttrib>mjtnet.com,DIV,classname=text,O,texts
Let>k=0
Repeat>k
  Let>k=k+1
  Let>this_text=texts_%k%
  MessageModal>this_text
Until>k=texts_count
Works fine.

So there's something else going on here. Are you able to send me some code which replicates the issue which I am able to run here?
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

NickD
Pro Scripter
Posts: 58
Joined: Fri Sep 23, 2016 2:17 pm

Re: Regex on Arrays

Post by NickD » Tue Feb 13, 2018 6:48 pm

Looping through the array output by IEGetTagsByAttrib> works fine in my script, the problem occurs when I try to parse it with regex. I will pm you the script, probably a newbie mistake in there somewhere :roll:

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Regex on Arrays

Post by Marcus Tettmar » Wed Feb 14, 2018 9:48 am

You aren't checking to see if there is a match. So even if there is no match you're outputting something like titles_3 or whatever. Maybe you just need to see if there's a match. Maybe if there's no match you want to assign the output to the input (i.e. the raw string prior to the RegEx - I don't know - you know what you want to do).
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts