Shoutbox

Regex with Variables - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Regex with Variables (/showthread.php?tid=87357)

Regex with Variables by ArkaneArkade on 11-19-2008 at 06:05 AM

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
RE: Regex with Variables by Mnjul on 11-19-2008 at 10:32 AM

You can use normal strings as the constructor parameters of RegExp.

jscript code:
new RegExp("<img id=\"" + id + "\" src=\"(.*?)\">", "i");

RE: Regex with Variables by ArkaneArkade on 11-19-2008 at 12:26 PM

Thats absolutely perfect Mnjul, thank you so much.  :D


RE: Regex with Variables by ArkaneArkade on 11-19-2008 at 11:36 PM

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?
RE: Regex with Variables by andrewdodd13 on 11-20-2008 at 08:29 AM

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.


RE: Regex with Variables by ArkaneArkade on 11-20-2008 at 04:13 PM

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 :)


RE: Regex with Variables by ArkaneArkade on 11-21-2008 at 07:08 AM

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.
RE: Regex with Variables by Matti on 11-21-2008 at 04:41 PM

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. :)
RE: Regex with Variables by ArkaneArkade on 11-22-2008 at 11:51 AM

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.