
mhx
Enthusiast
Jul 18, 2001, 1:20 AM
Post #5 of 11
(31999 views)
|
Hi Pro_4, yes, there's a problem with your regex. The vertical bar | is the regex alternation operator. So you alternate nothing special and nothing special, and nothing special, of course, matches always. The regex engine advances one character and again finds a match for nothing special, and so on, so $msg will be filled with 0's afterwards, which of course then translate back to vertical bars. So, to have this fixed, escape the vertical bar: But all in all, you're going to have a problem with your solution: What if someone uses @'s or 0's in his posts all the time? You're going to store them as they are, but translate them back to +!+'s and |'s. One thing that comes to my mind right now (I guess there's plenty of better solutions) is to escape some of the special characters in your database. Test this piece of code:
$msg = <<'ENDMSG'; Hello Pro_4 | whoever! Here's my +!+ message +!+ for you! == \Marcus\ == ENDMSG $msg =~ s/([!=\\])/\\$1/g; $msg =~ tr/|/=/; print $msg; $msg =~ s/\\(.)|=/$1||'|'/ge; print $msg; The first part will replace all occurrences of !, = or \ with \!, \= or \\. So all possible +!+ sequences will be converted to +\!+ and you don't have problems with these. It's not so easy with the |'s however. But since we have escaped all ='s, we can safely use = as a replacement for |. This is done with the tr/// line. The text now looks like this:
Hello Pro_4 = whoever\! Here's my +\!+ message +\!+ for you\! \=\= \\Marcus\\ \=\= The next regex is for reversing these changes so you get the original message back. It will look for an escaped character OR an equal sign =. If it finds an escaped character, it removes the backslash, if it finds the equal sign, it is replaced by a vertical bar. I'm more than sure there are better solutions, but this one was the first that came to my mind. Hope this helps anyway. -- Marcus
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= ($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"
|