• Publicidad

Problemas con Regexp de Perl

¿Apenas comienzas con Perl? En este foro podrás encontrar y hacer preguntas básicas de Perl con respuestas aptas a tu nivel.

Problemas con Regexp de Perl

Notapor Iojis » 2006-03-26 14:11 @632

Hola, tengo un problema.

Estoy haciendo un sistema de smileys en un foro hecho en Perl y tengo un drama con el código de sustitución.

Por ej., quiero hacer que al poner ':)' se reemplace por una carita. Pero no entiendo bien el tema de poner las '\' para que no tome a los ':' y ')' como símbolos de Perl... ¿me entienden?

Este es el código que tengo:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. $body =~ s/[b]#1[/b]/[img src="http:\/\/path4x4.com\/bullets\/feliz.gif"]/g;
Coloreado en 0.003 segundos, usando GeSHi 1.0.8.4

La parte que está en negrita es lo que tengo actualmente para que sea reemplazado por la carita. Pero quiero que sea ':)'.

¿Cómo hago?

MIL GRACIAS

iojis
Path4x4.com
Iojis
Perlero nuevo
Perlero nuevo
 
Mensajes: 9
Registrado: 2006-03-26 14:06 @629

Publicidad

Notapor creating021 » 2006-03-26 15:15 @677

Podes hacer esto:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. $url = 'img src="http://path4x4.com/bullets/feliz.gif"';
  2. $body =~ s/#1/$url/g;
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4

o como lo tenías pero:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. $body =~ s/#1/img src="http:\/\/path4x4.com\/bullets\/feliz.gif"/g;
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4
Expect the worst, is it the least you can do?
Avatar de Usuario
creating021
Perlero frecuente
Perlero frecuente
 
Mensajes: 595
Registrado: 2006-02-23 16:17 @720
Ubicación: Frente al monitor

Re: Problemas con Regexp de Perl

Notapor explorer » 2006-03-26 16:19 @722

Esta es una forma de hacerlo.

Consiste en primero meter el smile en una variable escalar que luego utilizaremos para hacer la sustitución.

Para evitar que Perl se fije en los caracteres extraños, "escapamos" todo el contenido de la variable rodeándola con "\Q" y "\E", cuyo efecto es el de "escapar" los caracteres extraños poniéndoles automáticamente un '\' delante a cada uno de ellos.
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. foreach my $smile ( @smiles ) {    # :)
  2.  
  3.     $carita = $imgcaritas{$smile}; # feliz.gif
  4.     $nuevosmile = "\Q$smile\E";    # \:\)
  5.  
  6.     # Sustitución de todos(g) los $nuevosmiles en $body por nuestras $caritas
  7.     $body =~ s {$nuevosmile} {[img src="http://path4x4.com/bullets/$carita"]}g;
  8. }
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4

Otra forma es usando la función quotemeta, que... hace lo mismo...
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1.     $nuevosmile = quotemeta($smile); # \:\)
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


Más información de "\Q\E", al final de la sección Expresiones regulares.
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14480
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Emoticones en foro Perl

Notapor Iojis » 2006-03-26 17:42 @779

¿Qué tal de nuevo...? Gracias por la ayuda brindada anteriormente, pero debo decir que no supe arreglarla según mis necesidades. Es por eso que estoy pensando en soluciones alternativas.

La primera que se me ocurrió es la que tienen muchos foros actualmente, un pop-up donde uno elija la imagen que quiere, y ésta automáticamente es trasladada a la casilla de texto donde está el mensaje.

Yo estoy buscando un explicación a esto en los tutoriales, pero no la encuentro.

Me serviría de mucho una ayuda u orientación...

Mil gracias

Iojis
Path4x4.com

P.D.: Si quieren pueden ver el foro en Path4x4.com
Iojis
Perlero nuevo
Perlero nuevo
 
Mensajes: 9
Registrado: 2006-03-26 14:06 @629

Re: Emoticons en foro PERL...

Notapor explorer » 2006-03-27 06:24 @308

Quizás si nos dieses más pistas de qué es lo que te pasaba...

En este foro se hace el mismo truco. Las caras se ponen como caracteres especiales o como palabras especiales, y luego, en el php que genera la página, se cambia a la imagen del emoticón.

Pero lo que se puede hacer en PHP en directo, también se puede hacer en Perl...

¿No te funcionó lo que te mandé?
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14480
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

re: emoticones en Perl

Notapor Iojis » 2006-03-27 16:39 @735

No sé si no lo supe usar o cuál fue el drama, pero no me funcionó.

Lo que quiero hacer es lo que está hecho en phpBB el mismo sistema de emoticones, pero no puedo pensar de qué manera hacer que se traslade la imagen o el código :) al text box.

Este es el código que tengo hasta ahora...
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1.    $body =~ s/\:\)/[img src="http:\/\/path4x4.com\/bullets\/feliz.gif"]/g;
  2.  
  3.    $body =~ s/\:\(/[img src="http:\/\/path4x4.com\/bullets\/triste.gif"]/g;
  4.  
  5.    $body =~ s/d:/[img src="http:\/\/path4x4.com\/bullets\/boina.gif"]/g;
  6.  
  7.    $body =~ s/:S/[img src="http:\/\/path4x4.com\/bullets\/confundido.gif"]/g;
  8.  
  9.    $body =~ s/#16/[img src="http:\/\/path4x4.com\/bullets\/risa.gif"]/g;
  10.  
  11.    $body =~ s/#10/[img src="http:\/\/path4x4.com\/bullets\/guino.gif"]/g;
  12.  
  13.    $body =~ s/#19/[img src="http:\/\/path4x4.com\/bullets\/verguenza.gif"]/g;
  14.  
  15.    $body =~ s/#23/[img src="http:\/\/path4x4.com\/bullets\/exclamacion.gif"]/g;
  16.  
  17.    $body =~ s/#22/[img src="http:\/\/path4x4.com\/bullets\/pregunta.gif"]/g;
  18.  
  19.    $body =~ s/#5/[img src="http:\/\/path4x4.com\/bullets\/contento.gif"]/g;
  20.  
  21.    $body =~ s/#6/[img src="http:\/\/path4x4.com\/bullets\/cool.gif"]/g;
  22.  
  23.    $body =~ s/#24/[img src="http:\/\/path4x4.com\/bullets\/flecha.gif"]/g;
  24.  
  25.    $body =~ s/#7/[img src="http:\/\/path4x4.com\/bullets\/decepcionado.gif"]/g;
  26.  
  27.    $body =~ s/#8/[img src="http:\/\/path4x4.com\/bullets\/diablo.gif"]/g;
  28.  
  29.    $body =~ s/#18/[img src="http:\/\/path4x4.com\/bullets\/sorprendido.gif"]/g;
  30.  
  31.    $body =~ s/#17/[img src="http:\/\/path4x4.com\/bullets\/sonrisa.gif"]/g;
  32.  
  33.    $body =~ s/#15/[img src="http:\/\/path4x4.com\/bullets\/pensativo.gif"]/g;
  34.  
  35.    $body =~ s/#13/[img src="http:\/\/path4x4.com\/bullets\/neutral.gif"]/g;
  36.  
  37.    $body =~ s/#14/[img src="http:\/\/path4x4.com\/bullets\/mrgreen.gif"]/g;
  38.  
  39.    $body =~ s/#11/[img src="http:\/\/path4x4.com\/bullets\/lloron.gif"]/g;
  40.  
  41.    $body =~ s/#12/[img src="http:\/\/path4x4.com\/bullets\/loco.gif"]/g;
  42.  
  43.    $body =~ s/#3/[img src="http:\/\/path4x4.com\/bullets\/increible.gif"]/g;
  44.  
  45.    $body =~ s/#21/[img src="http:\/\/path4x4.com\/bullets\/idea.gif"]/g;
  46.  
  47.    $body =~ s/#9/[img src="http:\/\/path4x4.com\/bullets\/enojado.gif"]/g;
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4

Es demasiado tedioso manejar eso, es un lio de código para hacer una pavada, por eso quiero acortar un poco el código buscando formas alternativas.

Gracias por la ayuda.

iojis.
Path4x4.com
Iojis
Perlero nuevo
Perlero nuevo
 
Mensajes: 9
Registrado: 2006-03-26 14:06 @629

Notapor kidd » 2006-03-27 17:20 @764

Hola.

Tienes razón, es demasiado complicado como lo tienes y muy difícil de mantener, así que se me ocurre una solución:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. # Declara aquí todos tus emoticones
  2. # USO:
  3. #             [símbolo] => [imagen]
  4. my %Emoticon =(
  5.                ':)'   => 'feliz.gif',
  6.                ':-)'  => 'feliz.gif',
  7.                ':S'   => 'confundido.gif',
  8.                '#16'  => 'risa.gif',
  9.                '#10'  => 'guino.gif'
  10.                );
  11.  
  12.  
  13. # El URL a las imágenes de tus emoticones
  14. my $emoticon_url = 'http://path4x4.com/bullets/';
  15.  
  16. # Y después ya puedes hacer:
  17.  
  18. # Sustituir los emoticones
  19. for my $key (keys %Emoticon){
  20.     my $code = "[img src=\"$emoticon_url"."$Emoticon{$key}\"]";
  21.     $body =~ s{\Q$key\E}{$code}gxsm;
  22. }
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4


Saludos
Uriel Lizama Perl programmer fundador de Perl en Español
Perl Programming Language
Avatar de Usuario
kidd
Creador de Perl en Español
Creador de Perl en Español
 
Mensajes: 1166
Registrado: 2003-10-15 16:52 @744
Ubicación: México

buenísimo...

Notapor Iojis » 2006-03-27 18:41 @820

Me encantó la idea... Ahora me voy a poner a probar... Mil gracias.

Iojis
Path4x4.com
Iojis
Perlero nuevo
Perlero nuevo
 
Mensajes: 9
Registrado: 2006-03-26 14:06 @629

..

Notapor Iojis » 2006-03-27 18:48 @825

Excelente, funcionó a la perfección... ¡MUCHAS GRACIAS!

Iojis
Path4x4.com
Iojis
Perlero nuevo
Perlero nuevo
 
Mensajes: 9
Registrado: 2006-03-26 14:06 @629


Volver a Básico

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 34 invitados

cron