What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » [Request] A simple regular expression function

[Request] A simple regular expression function
Author: Message:
Skarbo
New Member
*


Posts: 7
38 / Male / Flag
Joined: Apr 2008
O.P. [Request] A simple regular expression function
Im not any good at building regular expression so i need a litle help with this simple function.

All i want to do is search for a character in a string and move it to the end of the string.

Example:

function moveToEnd(string, search) ->
moveToEnd(AABBCCDDEE, A)

returns

BBCCDDEEAA (moves all the A's to the end)
04-28-2008 09:33 AM
Profile E-Mail PM Find Quote Report
markee
Veteran Member
*****

Avatar

Posts: 1621
Reputation: 50
35 / Male / Flag
Joined: Jan 2006
RE: [Request] A simple regular expression function
If they are all grouped together:

code:
StringObj.replace(/(.*)(A*)(.*)/i,"$1$3$2);

I personally can't think of a way to get all of them on the end however... This code might work but t is untested...

code:
var StringObj;
while(RegExp(".*A+[^A]+A*$","gi").exec(StringObj)){
    StringObj.replace(/(.*?)(A*)(.*)/i,"$1$3$2);
}

[Image: markee.png]
04-28-2008 11:14 AM
Profile PM Find Quote Report
John Anderton
Elite Member
*****

Avatar

Posts: 3908
Reputation: 80
37 / Male / Flag
Joined: Nov 2004
Status: Away
RE: [Request] A simple regular expression function
Try this. Non regexp method though.
1) Take string (AABBCCDDEE) and char to replace (A)
2) Count instances of char to replace (A)
3) Create target string all chars not char to replace in string ie target string = "BBCCDDEE"
4) Append 'count' number of char to replace (ie As) at the end


Should be simple enough :-)
[

KarunAB.com
]

[img]http://gamercards.exophase.com/459422.png[
/img]
04-28-2008 05:40 PM
Profile E-Mail PM Web Find Quote Report
markee
Veteran Member
*****

Avatar

Posts: 1621
Reputation: 50
35 / Male / Flag
Joined: Jan 2006
RE: [Request] A simple regular expression function
quote:
Originally posted by John Anderton
2) Count instances of char to replace (A)
That would require a for loop that has more (or at least equal - only when there are no instances of double leters) itterations than my while though.... and would probably be a while satement unles you checked each letter individually.

However your method is better for someone to learn how to do it themselves I guess.....
[Image: markee.png]
04-28-2008 08:48 PM
Profile PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
31 / Male / Flag
Joined: Apr 2004
RE: [Request] A simple regular expression function
Looks like some very untested code, yes. I can't seem to get it working whatsoever, it's crashing my Messenger every time I try to run this (infinite loop?):
code:
var StringObj = "Some shit with A's.";
while(RegExp(".*A+[^A]+A*$","gi").exec(StringObj)){
    StringObj = StringObj.replace(/(.*?)(A*)(.*)/i,"$1$3$2");
}
Could you try to find some time to get it working in some way? :)
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
04-29-2008 04:43 PM
Profile E-Mail PM Web Find Quote Report
Skarbo
New Member
*


Posts: 7
38 / Male / Flag
Joined: Apr 2008
O.P. RE: [Request] A simple regular expression function
I made a simple function for now (before your post John Anderton ;))

code:
// Move substring to end of string
function moveToEnd(string, substring) {
    var moveToEndI = 0;
    while(string.indexOf(substring) > -1) {
        string = string.replace(substring, "");
        moveToEndI++;
    }
   
    for (;moveToEndI > 0; moveToEndI--)
        string = string + "" + substring;
    return string;
}
04-29-2008 04:52 PM
Profile E-Mail PM Find Quote Report
markee
Veteran Member
*****

Avatar

Posts: 1621
Reputation: 50
35 / Male / Flag
Joined: Jan 2006
RE: [Request] A simple regular expression function
quote:
Originally posted by Mattike
Could you try to find some time to get it working in some way? :)
Got it working now

code:
function moveToEnd(oString,sub){
    var re = RegExp(".*?"+sub+"+(?!$)","gim");
    eString = oString;
    while(re.exec(oString) != null){
        eString = eString.replace(RegExp("(.*?)("+sub+"+)(.*)","gi"),"$1$3$2");
    }
    return eString;
}
This has FAR less itterations and should respond quicker.  Realy I should do error checking to make sure sub is only a single character, but if you are carful then there shouln't be any problem and will work faster.

I hope this helps :)

This post was edited on 04-30-2008 at 10:00 AM by markee.
[Image: markee.png]
04-30-2008 09:39 AM
Profile 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