Shoutbox

Problem with RegExp + Replace - 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: Problem with RegExp + Replace (/showthread.php?tid=66726)

Problem with RegExp + Replace by Montago on 09-27-2006 at 08:28 PM

i want to do something really simple - to locate numbers for calculation

function...
    var reg_exp = "([0-9\*\/+\-()]*)";

    if( Messege.match( new RegExp(reg_exp) ) ){
        return Messege.replace(new RegExp(reg_exp,'gi'),"[$1]");
    }

...

Using the function above, should do this :

input : "Havelåge 5+5+5-(5*6) sdsd"
output : "Havelåge [5+5+5-(5*6)] sdsd"

but instead it : "[]H[]a[]v[]e[]l[]å[]g[]e[] [5][]+[5][]+[5][]-[]([5][]*[6][])[] []s[]d[]s[]d[]"


what am i doing wrong ???


RE: Problem with RegExp + Replace by deAd on 09-27-2006 at 08:57 PM

Try using:

code:
return Message.replace(/[0-9\*\/+\-()]*/g, "[$1");

Also, I don't know a ton about regular expressions, but that one looks a bit flawed to me.
RE: Problem with RegExp + Replace by Silentdragon on 09-27-2006 at 09:24 PM

code:
Message.replace(/([0-9+\-()*\\\/]+)/,"[$1]")
Should work

deAd: $1-9 won't work unless you use (). So for each $ you need a set of ().
RE: Problem with RegExp + Replace by deAd on 09-27-2006 at 09:47 PM

Well there were parentheses, but they would always return a blank string -- that's why I said it looked flawed...but I didn't try to correct it because (1) not exactly sure what they're trying to accomplish, and (2) since I'm not so good at regular expressions, I figured I'd just leave it as it was..


RE: Problem with RegExp + Replace by Montago on 09-27-2006 at 10:45 PM

quote:
Originally posted by Silentdragon
code:
Message.replace(/([0-9+\-()*\\\/]+)/,"[$1]")
Should work


no way :)

/([0-9+\-()*\\\/]+)/

'+' after numeric gives trouble
')*' will look for more ')'
'\\' why look for backlash ?
']+' i guess *(start) is more appropriate ??

you are right about the () => $1 matching, which is what im looking for

i tried you solution didn't do what i wanted though, actually nothing... im trying a modified version

thanks anyway :)
RE: Problem with RegExp + Replace by deAd on 09-27-2006 at 10:49 PM

Actually, ")*" will not look for more ")"s, unless you specify that the ")" in that case is a character, and in that expression ")" is being used to end a sub-match (whatever you call it...).


RE: Problem with RegExp + Replace by Montago on 09-27-2006 at 10:54 PM

ohhh... that might explain something... if the ')' is un-escaped, it might make a submatch...

so, i need to escape the ')' :)


RE: Problem with RegExp + Replace by deAd on 09-27-2006 at 11:03 PM

Yes :)

EDIT: What exactly are you trying to do?


RE: Problem with RegExp + Replace by Montago on 09-27-2006 at 11:09 PM

i want to find calculations within a text string...

lets say i write
"hello jim, i have 25*20 dollars"
then my plugin calculated the amount -->
"hello jim, i have 500 dollars"

so i want to match simple math pieces


RE: Problem with RegExp + Replace by deAd on 09-27-2006 at 11:54 PM

Well then I think you're going about it wrong. Here is a better way to do it. The code, however, is a bit messy. This will evaluate division, multiplication, addition, and subtraction (/, *, +, -). It uses the eval() function. It accepts decimals and disregards whitespace. However, there is only support for problems with two numbers (3*4*5 won't work).

code:
var Str = "Did you know that 5 time 5 is 5*5? Did you know that 8 plus 8 is 8+8?";

    var RegExp = /[\d.]+[\s]*[*/+-][\s]*[\d.]+/g;

    Str = Str.replace(RegExp, function(){ var res = eval(RegExp.exec(Str)[0]); RegExp.lastIndex++; return res; } );

RE: Problem with RegExp + Replace by Shondoit on 09-28-2006 at 04:57 AM

He had the right solution
Yours doesn't take into account for more numbers (as you said so yourself)

This should work, in red are the changes:

code:
var reg_exp = /([0-9*/+\-()]+)/gi;
//brackets indicate a set of characters, you don't need to escape anything in here except for the minus sign, which would indicate a range of characters
document.write(Messege.replace(reg_exp,"[$1]"));


Especialy the little plus at the end of the RegExp is important because it indicates that it need one or more matches..., instead of the asterisk which indicates zero or more matches, so in this case, every single character. That's why you had that problem...

If you have any further question or want something explained, just PM me
RE: Problem with RegExp + Replace by Montago on 09-28-2006 at 11:14 AM

thanks Shondoit - works a bit better

now i need to refine the regexp to capture mathpieces better - currently smileys are also captured because of ')'

so im thinking that a math piece is constructed by:
- a number of brackets '()' zero or more
- at least 2 numbers
- at least one operator '/*-+^'

a math rule is that 2(5+4) == 2*(5+4) so this one needs to be captured aswell

a better choice then looking for mathpieces is to let the user mark a piece himself... with corner brackets '[]' which is many times simpler

Next - how do i EVAL the piece ?? earlier in the thread, a function was written - this is a bit unhandy - and i recall that the function would only process one math piece ?


RE: Problem with RegExp + Replace by CookieRevised on 09-29-2006 at 12:25 AM

quote:
Originally posted by Montago
Next - how do i EVAL the piece ??
like exactly like you just said: use the Eval() function in the replace.

quote:
Originally posted by Montago
and i recall that the function would only process one math piece ?
the replace can replace all occuring findstrings.

Search the forum for "calc", there are already a few threads about calculating  expressions. So to avoid repeating everything which has been said already in other threads, search forums first and you'll find similar threads..

- calculating with eval(). eg:
CookieRevised's reply to An idea for a script

- one which explanes how the Replace method actually works (instead of "$1" as replacement string, you can use a function as a replacement string. This is what makes Replace so powerfull and which makes it possible to use Eval for example directly in your replace method. eg:
CookieRevised's reply to [Question] Isn't string.replace supposed to replace all occurences ?
(^^ explains the replace function)
CookieRevised's reply to Replace Colour Codes
(^^ shows an example for replace using a function as replace text)
CookieRevised's reply to [I help them] VB2JS
(^^ shows an example for replace using a function as replace text)

- one which uses a complex algorithm instead of eval() to calculate almost all possible expressions (eval() is rather limited). eg:
[?] Pow... err, triggering some function on certain convo events?

- etc...

RE: Problem with RegExp + Replace by deAd on 09-29-2006 at 12:34 AM

Note that you can't do:

code:
Str.replace(RegExp,eval("$1"));

RE: Problem with RegExp + Replace by CookieRevised on 09-29-2006 at 12:41 AM

quote:
Originally posted by deAd
Note that you can't do:
code:
Str.replace(RegExp,eval("$1"));

if that was in reply to my post, then read the threads I linked to, or look up the information on the replace function in the JScript help files (on msdn, in the JScript help file, etc)...

RE: Problem with RegExp + Replace by deAd on 09-29-2006 at 12:43 AM

It wasn't in reply, it was just a note :P
EDIT: Also, I posted an example of using a function in String::replace earlier in this thread...just that my regex was a bit off..


RE: Problem with RegExp + Replace by CookieRevised on 09-29-2006 at 12:51 AM

quote:
Originally posted by deAd
It wasn't in reply, it was just a note :P
EDIT: Also, I posted an example of using a function in String::replace earlier in this thread...just that my regex was a bit off..
your function was a bit off too...

it worked but you let replace search with a regular expression and when it found a match you searched the string again for that same match, while it already found the match in the first place. In short: your function didn't made much sense logically (aka doesn't teach the proper thing). What you did was like asking a doctor to examine you, and when he lists the problems, you say: screw it, I'll search it myself using that medical reference... :p

;)

correct way:

code:
var s = "Hello there, 1+1 should be 2. And 5-2 will be 3.";
s = s.replace(/([0-9*/+\-()]+)/gi, function($0, $1) { return eval($1) });
Debug.Trace(s);

RE: Problem with RegExp + Replace by Montago on 09-29-2006 at 09:33 AM

var s = "Hello there, 1+1 should be 2. And 5-2 will be 3.";
s = s.replace(/([0-9*/+\-()]+)/gi, function($0, $1) { return eval($1) });
Debug.Trace(s);

did the trick :)

now im able to do some more advanced stuff upon those expressions that are found...

also not-find simple chars... when i write a bare number, it is eval'ed so i gotta fix this :)