CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Replacement problem

 



yapp
User

Feb 14, 2002, 1:58 PM

Post #1 of 6 (6370 views)
Replacement problem Can't Post

Well, I have a problem with regexps. again.

I want to replace a part of the text, if and only if the character ; is not used before it.

Actually, my smiley's get messed up. Some ASCII codes are converted to HTML escape codes first, like a > becomes a > Putting a ) after that makes the smiley convertor thing believe that's a wink smiley: ">)"

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool


fashimpaur
User

Feb 14, 2002, 2:20 PM

Post #2 of 6 (6368 views)
Re: [yapp] Replacement problem [In reply to] Can't Post

Something like:

s/[^;]./$replacevalue/g;

where $replacevalue is whatever you want to replace it with.
Did not try this. Spun it off the top of my head. Good Luck.

HTH,
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


yapp
User

Feb 15, 2002, 6:33 AM

Post #3 of 6 (6361 views)
Re: [fashimpaur] Replacement problem [In reply to] Can't Post

No that didn't help. ALthough I have a clue.

- first $message = "Hello>)"
- it get's converted to HTML: $mesage = "Hello&gt;)"
- the ;) smiley is replaced: $message = "Hello&gt<IMG src=....>"

I can't swap step 2 and 3, since step 4 inserts HTML code into the post. If I would execute thep 2 at later, the smiley <IMG> tag would be converted to a &lg;IMG&gt; code, so the HTML code is being displayed.

So actually, I need a regexp that converts the ;) smiley, unless a the regexp /\&[a-z]+/ is found before it. Do you have any ideas for that?

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool


fashimpaur
User

Feb 15, 2002, 12:33 PM

Post #4 of 6 (6354 views)
Re: [yapp] Replacement problem [In reply to] Can't Post

How about this:

$message = "Hello>) <> Welcome to Perl ;)";

# If I am not mistaken, you want this for output:
#$message = "Hello&gt;) &lt;&gt; Welcome to Perl <img src="wink.gif">";

#How about this:

$message =~ s/>/&gt;/gm;
$message =~ s/</&lt;/gm;

print $message."\n";

$message =~ s/[^\&a-z{2}]{1};\)/ <img src="wink.gif">/;
print $message."\n";

Is that what you want?
HTH,
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


yapp
User

Feb 16, 2002, 7:29 AM

Post #5 of 6 (6347 views)
Re: [fashimpaur] Replacement problem [In reply to] Can't Post

CoolCool. That definitely works! Smile



I didn't use the [^ ] character class, since the sequence of the characters in there don't matter. However your {2} trick in that class is very cool, and I didn't knew about it.

Why so you use the /m modifier to replace the < and > codes? I'll test if that is faster, which is a good reason to use it.

I've been trying something with the /e modifier, so a function can perform a conditional test on the $1 variable filled by the regexp. That worked aswell, but maybe your solution is even better (or faster Wink)

p.s. I assume you forgot to put the /g modifier at the end of the s/// regexp...?

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool


fashimpaur
User

Feb 18, 2002, 6:59 AM

Post #6 of 6 (6337 views)
Re: [yapp] Replacement problem [In reply to] Can't Post

Oops. You are correct. I should have used the /g operator. Good Luck.
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives