What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » [HELP]match New question Solved!

[HELP]match New question Solved!
Author: Message:
DaAniv
Junior Member
**


Posts: 19
30 / Male / Flag
Joined: Jan 2009
O.P. [HELP]match New question Solved!
code:
for(i=0;i==0;i=i){
    if (sentenceT.match(/\[t=.+?\]/i)=="[t=ps]")
        {
            Time=sentenceT.match(/\[t=.+?\].+?\[\/t\]/i)
            Debug.Trace(Time)
            sentenceT=sentenceT.replace((/\[t=.+?\].+?\[\/t\]/i),"")
           
        }
    else {i=1}}

well if finds me the string I want but I want Time to equal the string between the [t],[/t] and I don't really know how anyone can help?

This post was edited on 01-25-2009 at 12:53 PM by DaAniv.
01-24-2009 09:59 AM
Profile E-Mail PM Find Quote Report
Danny22
New Member
*


Posts: 11
Joined: Dec 2008
RE: [HELP]match
Maybe this is what you're trying to do?
code:
Debug.Trace(Time[1])
If not, please let me know.
01-24-2009 10:16 AM
Profile PM Find Quote Report
DaAniv
Junior Member
**


Posts: 19
30 / Male / Flag
Joined: Jan 2009
O.P. RE: [HELP]match
no Time=[t=ps]bla bla[/t] and I want Time="bla bla"
01-24-2009 11:10 AM
Profile E-Mail PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
31 / Male / Flag
Joined: Apr 2004
RE: [HELP]match
Aah, here comes on of the most beautiful features about JScript's replace function: you can use a regular expression combined with a custom replacement handler function!

First of all, you need to put what you want to find between parentheses in your regular expression. This way, the engine will capture these parts and pass them as parameters to your replace function or as properties of the global RegExp object (e.g. RegExp.$1). This will give you almost infinite control about how the replacing is done since you can do anything inside your handler, and it is something I use a lot in my scripts too.

Here is your code adapted with String.replace. As you can see, I got rid of your for-loop (in fact, you were better off with a while-loop there) and I made an example replacement handler function.
Javascript code:
sentenceT = sentenceT.replace(
    /\[t=(.+)\](.*?)\[\/t\]/gi, //The regular expression, note the parentheses to capture parts of our interest
    function(sMatch, $1, $2, nOffset, sFull) {  //We can use a function to do the replacing!
        if($1 === "ps") {
            //We found a [t=ps] match
            Time = $2;  //Set the Time variable to what's between the tags
            return "";  //Return an empty string to remove this match from the result
        }
        //Return the matched piece untouched if nothing was recognized
        return sMatch;
    }
);

Of course, if you have for instance 3 capture parts in your regular expression, you should place 3 receiving parameters in your handler function. Alternatively, you can leave out the last parameters (nOffset and sFull) if you don't need them.
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
01-24-2009 11:11 AM
Profile E-Mail PM Web Find Quote Report
DaAniv
Junior Member
**


Posts: 19
30 / Male / Flag
Joined: Jan 2009
O.P. RE: [HELP]match
Thanks a lot it works
01-24-2009 11:22 AM
Profile E-Mail PM Find Quote Report
Danny22
New Member
*


Posts: 11
Joined: Dec 2008
RE: [HELP]match Solved
Sorry, when I said Time[1], I also meant this:
code:
Time = sentenceT.match(/\[t=.+?\](+?).\[\/t\]/i)
But it seems like you got your answer already. :)

This post was edited on 01-24-2009 at 11:39 AM by Danny22.
01-24-2009 11:38 AM
Profile PM Find Quote Report
DaAniv
Junior Member
**


Posts: 19
30 / Male / Flag
Joined: Jan 2009
O.P. RE: [HELP]match new question
code:
a = Math.floor((Math.random()*(Nouns.length-1)));
b = Math.floor((Math.random()*(VerbsES.length-1)));
d = Math.floor(((Math.random()*16)+14));
e = Math.floor((Math.random()*(Places.length-1)));
f = Math.floor((Math.random()*(PlacesPrepos.length-1)));
g = Math.floor((Math.random()*(Adjectives.length-1)));
//person
if (string.match(/!person/i)){
    c = Math.floor((Math.random()*(Person.length-1)));
    string=string.replace(/!person/i, Person[c])}
//verb
if (!(c==0||c==1||c==2||c==3||string.match(/!i/i)||string.match(/!you/i)||string.match(/!we/i)||string.match(/!they/i)))
    string=string.replace(/!verbs/, VerbsES[b])
else
{
    string=string.replace(/!verbs/i, Verbs[b])
    string=string.replace(/!/g,"")
}
well I want It replace more then one time but every time different number
e.g
string=!person !person
and every person is different but I don't want to use useless loops or anything

This post was edited on 01-24-2009 at 12:25 PM by DaAniv.
01-24-2009 12:19 PM
Profile E-Mail PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
31 / Male / Flag
Joined: Apr 2004
RE: [HELP]match New question
Well, as I said, you can do anything you want in your replacement handler! Only this time, we don't need any of the parameters passed to our handler function.
Javascript code:
var c;
string = string.replace(/!person/i, function() {
    c = Math.floor((Math.random()*(Person.length-1)));
    return Person[c];
});

From what I understand of your script now, you're working on a random sentence generator. The problem now is that you're replacing "!person" more than once and you need to find the correct conjugation of the verb for each instance of a person, which may be a tough challenge...
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
01-24-2009 12:49 PM
Profile E-Mail PM Web Find Quote Report
DaAniv
Junior Member
**


Posts: 19
30 / Male / Flag
Joined: Jan 2009
O.P. RE: [HELP]match New question
ya spot on but It needs to set a verb after the person and then come back to check if theres a new !person
01-24-2009 01:44 PM
Profile E-Mail PM Find Quote Report
DaAniv
Junior Member
**


Posts: 19
30 / Male / Flag
Joined: Jan 2009
O.P. RE: [HELP]match Solved
Solved D: thanks to everyone who helped me^_^
Javascript code:
//person
            string.replace(/!person/mig, function()
                    {
                        c = Math.floor((Math.random()*(Person.length-1)));
                        string = string.replace(/!person/mi, Person[c])
                        //verb
                    string = string.replace(/(.*?)!person/mi,function(sMatch, $1)
                    {
                        substr=$1
                        if (!(c==0||c==1||c==2||c==3||string.match(/!i/mi)||string.match(/!you/mi)||string.match(/!we/mi)||string.match(/!they/mi)))
                            substr=substr.replace(/!verbs/mgi,function()
                            {  
                            b = Math.floor((Math.random()*(VerbsES.length-1)));
                            return VerbsES[b]})
                        else
                            substr=substr.replace(/!verbs/mgi,function()
                            {  
                            b = Math.floor((Math.random()*(Verbs.length-1)));
                            return Verbs[b]})
                        return substr+"!person"
                    });
                    return string
                });
            //verb
            string = string.replace(/!verbs/mgi,function()
            {
                b = Math.floor((Math.random()*(VerbsES.length-1)));
                if (!(c==0||c==1||c==2||c==3||string.match(/!i/mi)||string.match(/!you/mi)||string.match(/!we/mi)||string.match(/!they/mi)))
                    return VerbsES[b]
                else
                    return Verbs[b]
            });


This post was edited on 01-25-2009 at 12:53 PM by DaAniv.
01-24-2009 02:49 PM
Profile E-Mail PM 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