What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Regex with Variables

Regex with Variables
Author: Message:
ArkaneArkade
Full Member
***

Avatar
The One and Only

Posts: 193
Reputation: 5
39 / Male / Flag
Joined: Mar 2007
O.P. Regex with Variables
Hey guys,

Its been a day, so I'm back again with another stupid question.  I'm looking for some help, and to find out if its possible to regex a variable.

I know the basic idea is to looks for a pattern, but I'm trying to get something with a little more choice.
Basically, in my script I have a radio box, which will set a variable to be either "One", "Two" or "Three" (yes, it's dumbed down, not because you guys need it, but its easier to explain).  I then download a webpage, which has various bits of info, including 3 images, each with an id of "One", "Two" or "Three". 
I've managed to use the regex (with my severely limited understanding) to download the image with id "One" using
jscript code:
new RegExp(/<img id="One" src="(.*?)">/i);
However, since I intend to add a lot more options for hte ids, I'm looking for a way to get them in the regexp as the variable, but I don't understand how that would be done, since I'm confused enough by the lack of quotes.

Can anyone help me?

Cheers as always guys,
Leroux
[Image: adsig.jpg]
11-19-2008 06:05 AM
Profile E-Mail PM Web Find Quote Report
Mnjul
forum super mod
******

Avatar
plz wub me

Posts: 5396
Reputation: 58
– / Other / Flag
Joined: Nov 2002
Status: Away
RE: Regex with Variables
You can use normal strings as the constructor parameters of RegExp.
jscript code:
new RegExp("<img id=\"" + id + "\" src=\"(.*?)\">", "i");
11-19-2008 10:32 AM
Profile PM Web Find Quote Report
ArkaneArkade
Full Member
***

Avatar
The One and Only

Posts: 193
Reputation: 5
39 / Male / Flag
Joined: Mar 2007
O.P. RE: Regex with Variables
Thats absolutely perfect Mnjul, thank you so much.  :D
[Image: adsig.jpg]
11-19-2008 12:26 PM
Profile E-Mail PM Web Find Quote Report
ArkaneArkade
Full Member
***

Avatar
The One and Only

Posts: 193
Reputation: 5
39 / Male / Flag
Joined: Mar 2007
O.P. RE: Regex with Variables
Here was me thinking I'd manage ok for the rest of today, but no, more confusion.

The page I downloaded is a simple html list, but does not close its <li> tags.  Is there any way for me to detect for this?

html code:
<html>
<li>Show Title: Supernatural
<li>Title:Lazarus Rising
<li>Season:4
<li>Episode:1</html>

I tried new RegExp("<li>Filename:(. * ?)", "i")); for it, but that doesnt work, because the ? make it take nothing since theres no ending match.  Any possibilites?

This post was edited on 11-19-2008 at 11:37 PM by ArkaneArkade.
[Image: adsig.jpg]
11-19-2008 11:36 PM
Profile E-Mail PM Web Find Quote Report
andrewdodd13
Senior Member
****

Avatar
Oh so retro

Posts: 870
Reputation: 16
34 / Male / Flag
Joined: Jan 2005
RE: Regex with Variables
Either detect the new line "\n", or detect the next <li> tag and... ah. You can detect the next <li> tag, and trim it from the string. But if you're doing a global match you will run into some problems.
[Image: AndrewsStyle.png]
11-20-2008 08:29 AM
Profile E-Mail PM Web Find Quote Report
ArkaneArkade
Full Member
***

Avatar
The One and Only

Posts: 193
Reputation: 5
39 / Male / Flag
Joined: Mar 2007
O.P. RE: Regex with Variables
Cheers man.

I've tried using both the next <li> (actually, I used just < to cover the html as well), and the \n,, but neither seemed to work - \n was causing some problem in debug, but I can't remember it now.
On the other hand, I removed the ? from the regexp to take everything, and it automatically only takes up to the line break, so it's all working now :)
[Image: adsig.jpg]
11-20-2008 04:13 PM
Profile E-Mail PM Web Find Quote Report
ArkaneArkade
Full Member
***

Avatar
The One and Only

Posts: 193
Reputation: 5
39 / Male / Flag
Joined: Mar 2007
O.P. RE: Regex with Variables
Now I've got another problem, and I don't have a clue what it is this time.  :S

I'm trying to get info from a contacts PSM, using RegEx.  I have for example
Xbox 360: Sonic The Hedgehog 2 (Stefan Leroux)
For this I'm using
jscript code:
var XBG = NewPsm.match(new RegExp("Xbox 360: (.*?) (" + X360GamerTag + ")", "i"))[1];
As far as I can figure, this should give me the game name, as it has a leading and ending space.  It just gives me a null or not an object in debug.
Even stranger is I changed it to remove the space between (.*?) and (, and this works, buts gives me a result of "Sonic The Hedgehog 2 (", including the (, which should be regexed out.
I've also tried \escaping the ( and ), but that just gave me the same error as the first.
Anyone able to tell me what I'm doing wrong?


- Q2) Slightly off topic, but I didn't want to start a new post.  How can I detect if an array has data?  I have a variable being set to regex, so I'm looking to set reply[1], but if there is no match, it just breaks.  I've tried if (reply[1]!==null) but it still gives me a debug of 'null' is null or not an object.

This post was edited on 11-21-2008 at 07:51 AM by ArkaneArkade.
[Image: adsig.jpg]
11-21-2008 07:08 AM
Profile E-Mail PM Web Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: Regex with Variables
It's because you have to escape the parentheses around the gamer tag with a backslash, but as you're creating a RegExp with a string, you first have to escape the backslash itself too. :P
code:
var XBG = NewPsm.match(new RegExp("Xbox 360: (.*?) \\(" + X360GamerTag + "\\)", "i"))[1];
The reason for this is if you don't escape the parentheses, it'll capture what's between those too:
code:
Resulting RegExp: Xbox 360: (.*?) (Stefan Leroux)
Result:
  [0] = Xbox 360: Sonic The Hedgehog 2 Stefan Leroux
  [1] = Sonic The Hedgehog 2
  [2] = Stefan Leroux
  • The matched part is stored at index 2, even when you already knew that part as you provided that data yourself!
  • It won't look for parentheses, it looks for "Stefan Leroux" immediately after "Sonic The Hedgehog 2".
So what did we learn today? Escape any characters which can be interpreted by the regular expression engine as special characters. :)

As for your second question: check if "reply" is null first before checking "reply[1]". That should do the trick. :)

This post was edited on 11-21-2008 at 04:48 PM by Matti.
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
11-21-2008 04:41 PM
Profile E-Mail PM Web Find Quote Report
ArkaneArkade
Full Member
***

Avatar
The One and Only

Posts: 193
Reputation: 5
39 / Male / Flag
Joined: Mar 2007
O.P. RE: Regex with Variables
Cheers Matti.  Everything working great now, except for me :S  I don't really understand why you need to escape a backslash to escape bracket, but not matter - either way I got it working now.
[Image: adsig.jpg]
11-22-2008 11:51 AM
Profile E-Mail PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On