Oh dios odio perl - Bloop Bloop! [Resuelto]

  • alex89
  • Bronze Member
  • Bronze Member
  • Avatar de Usuario
  • Registrado: Jul 18, 2008
  • Mensajes: 239
  • Loc: Western Australia
  • Status: Offline

Nota Octubre 4th, 2009, 7:46 am

Im tratando de reemplazar la cadena entre paréntesis foro "[pescado] Texto aquí [/ pescado]" por "boop! Boop t! E boop! X boop! Boop th! E boop! R boop! E"

Mi intento parece tener algunos problemas. ¿Alguna idea?

PERL Código: [ Select ]
my $message = "start [fish]middle[/fish] end \n newline";
 
my $fish = $message;
print "\n1" . $message;
print "\n2" . $fish;
$fish =~ s~\[fish\](.+?)\[/fish\]~~isg;
 
print "\n3: " . $fish;
 
my $newfish;
 
for ($count = 0; $count < length ($newfish); $count++){
 
 $newfish .= substr ($fish, $count, 1)."blop!";
 
}
 
print $newfish;
 
$message =~ s~\[fish\](.+?)\[/fish\]~$newfish~isg;
 
print $message;
  1. my $message = "start [fish]middle[/fish] end \n newline";
  2.  
  3. my $fish = $message;
  4. print "\n1" . $message;
  5. print "\n2" . $fish;
  6. $fish =~ s~\[fish\](.+?)\[/fish\]~~isg;
  7.  
  8. print "\n3: " . $fish;
  9.  
  10. my $newfish;
  11.  
  12. for ($count = 0; $count < length ($newfish); $count++){
  13.  
  14.  $newfish .= substr ($fish, $count, 1)."blop!";
  15.  
  16. }
  17.  
  18. print $newfish;
  19.  
  20. $message =~ s~\[fish\](.+?)\[/fish\]~$newfish~isg;
  21.  
  22. print $message;


Lo mismo en JavaScript y #058;

JAVASCRIPT Código: [ Select ]
var fish = message.match(/\[fish\](.+?)\[\/fish\]/ig);
 
if(fish != null) {
 
fish = fish[0];
 
fish = fish.replace( "[fish]", "");
fish = fish.replace( "[/fish]" , "");
 
var newfish = "";
 
for (var counter = 0;counter < fish.length ;counter ++ ) {
   newfish += fish.substring(counter-1, counter) + "bloop bloop!";
}
 
message = ubbcstr.replace(/\[fish\](.+?)\[\/fish\]/ig, newfish);
 
}
  1. var fish = message.match(/\[fish\](.+?)\[\/fish\]/ig);
  2.  
  3. if(fish != null) {
  4.  
  5. fish = fish[0];
  6.  
  7. fish = fish.replace( "[fish]", "");
  8. fish = fish.replace( "[/fish]" , "");
  9.  
  10. var newfish = "";
  11.  
  12. for (var counter = 0;counter < fish.length ;counter ++ ) {
  13.    newfish += fish.substring(counter-1, counter) + "bloop bloop!";
  14. }
  15.  
  16. message = ubbcstr.replace(/\[fish\](.+?)\[\/fish\]/ig, newfish);
  17.  
  18. }
  • Anonymous
  • Bot
  • No Avatar
  • Registrado: 25 Feb 2008
  • Mensajes: ?
  • Loc: Ozzuland
  • Status: Online

Nota Octubre 4th, 2009, 7:46 am

  • mk27
  • Proficient
  • Proficient
  • Avatar de Usuario
  • Registrado: Jun 09, 2009
  • Mensajes: 334
  • Status: Offline

Nota Octubre 4th, 2009, 7:35 pm

PERL ROCKS!

Un error que se encuentran atrapados en es que la segunda mitad de la sustitución tiene reglas de escape ligeramente diferente de la primera mitad. Por ejemplo, debe escapar de [] en la primera mitad, pero estos no tienen sentido en el segundo, así están muy bien como es menos que sigan una variable de cadena...adivinar por qué:

$ var [

Uh-oh! thats una suscripción matriz! En realidad no lo hace, pero está involucrado en el ejemplo siguiente, así que ten cuidado de ese detalle (línea 13).

Un error que hacen es que / se escapó en dos mitades, ya que es el divisor, aunque parece que usar \, por alguna razón - que está mal.

Así:
PERL Código: [ Select ]
#!/usr/bin/perl -w
use strict;
 
# simple replacement
my $string = "[fish]text here[/fish]\n";
$string =~ s/\[fish\].*\[\/fish\]/[fish]boop boop[\/fish]/g;
print $string;
 
# replacement involving intermediate process
$string = "[fish]text here[/fish]\n";
$string =~ /\[fish\](.*)\[\/fish\]/;
my $new = uc();
$string =~ s/\[fish\].*\[\/fish\]/[fish]$new\[fish]/g;
print $string;
 
  1. #!/usr/bin/perl -w
  2. use strict;
  3.  
  4. # simple replacement
  5. my $string = "[fish]text here[/fish]\n";
  6. $string =~ s/\[fish\].*\[\/fish\]/[fish]boop boop[\/fish]/g;
  7. print $string;
  8.  
  9. # replacement involving intermediate process
  10. $string = "[fish]text here[/fish]\n";
  11. $string =~ /\[fish\](.*)\[\/fish\]/;
  12. my $new = uc();
  13. $string =~ s/\[fish\].*\[\/fish\]/[fish]$new\[fish]/g;
  14. print $string;
  15.  


Usando un editor que tiene un buen resaltado de sintaxis para perl ayuda mucho con esto (el resaltado aquí es relativamente rudimentaria, y no cubre la expresión regular)...una alternativa para la línea 6 es la siguiente:
Código: [ Select ]
$string =~ s/(\[fish\]).*(\[\/fish\])/boop boop/g;
Imagen
  • alex89
  • Bronze Member
  • Bronze Member
  • Avatar de Usuario
  • Registrado: Jul 18, 2008
  • Mensajes: 239
  • Loc: Western Australia
  • Status: Offline

Nota Octubre 4th, 2009, 8:47 pm

Ah, gracias, ahora lo consigo.

Mi código final:

PERL Código: [ Select ]
$message=~ /\[fish\](.*)\[\/fish\]/;
my $fish = ; #I thought you could do this in one line
 
my $newfish;
 
for ($count = 0; $count < length ($fish); $count++){
   $newfish .= substr ($fish, $count, 1)." bloop bloop! ";
}
 
$message =~ s~\[fish\](.+?)\[/fish\]~$newfish~isg;
  1. $message=~ /\[fish\](.*)\[\/fish\]/;
  2. my $fish = ; #I thought you could do this in one line
  3.  
  4. my $newfish;
  5.  
  6. for ($count = 0; $count < length ($fish); $count++){
  7.    $newfish .= substr ($fish, $count, 1)." bloop bloop! ";
  8. }
  9.  
  10. $message =~ s~\[fish\](.+?)\[/fish\]~$newfish~isg;

Publicar Información

  • Total de mensajes en este tema: 3 mensajes
  • Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 169 invitados
  • No puede abrir nuevos temas en este Foro
  • No puede responder a temas en este Foro
  • No puede editar sus mensajes en este Foro
  • No puede borrar sus mensajes en este Foro
  • No puede enviar adjuntos en este Foro
 
 

© 2011 Unmelted, LLC. Ozzu® es una marca registrada de Unmelted, LLC