🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.os.linux.programacion
Article #5836

Re: Regexp & Perl: Como sustituir varias palabras parecidas

#5836
From: Francesco Moi
Date: Tue, 02 Sep 2003 14:13
97 lines
3839 bytes
Muchas gracias, Samuel. Ahora comprendo lo que
antes no entend�a.

Samuel <444398NOSPAM@celes.unizar.es> wrote in message news:<slrnbl8f1v.ks.444398NOSPAM@tizona.no-ip.org>...
> El 1 Sep 2003 15:31:14 -0700, Francesco Moi 
>  <francescomoi@europe.com> dijo:
> > Hola.
> > 
> > En Perl, tengo una cadena con un texto, en la que me gustar�a sustituir 
> > las palabras 'programacion', 'programa' o 'programador' por 'program'.
> > 
> > Con esta RegExp funciona:
> > $texto =~ s/programador|programa|programacion/program/g;
> > 
> > Pero como son palabras muy parecidas, pensaba que se podr�a hacer
> > mucho m�s facil, como por ejemplo:
> > $texto =~ s/program[ador,a,cion]/program/g;
> > $texto =~ s/program[ador|a|cion]/program/g;
> > 
> > Pero ninguna de las dos funciona. �Alguien sabe como hacer m�s corta
> > la RegExp, si es que se puede?
> > 
> > Gracias, y un saludo.
> 
> Creo que no has entendido como funciona el []. Pero tu mismo te puedes
> dar cuenta depurando la expresi�n regular. Veamos:
> 
> 1) Primer caso (Bueno)
> 
> $  perl -e  'use re 'debug'; \
>  $texto =~ s/programador|programa|programacion/program/g;'
> 
> Compiling REx `programador|programa|programacion'
> size 15 
>    1: BRANCH(6)
>    2:   EXACT <programador>(15)
>    6: BRANCH(10)
>    7:   EXACT <programa>(15)
>   10: BRANCH(15)
>   11:   EXACT <programacion>(15)
>   15: END(0)
> minlen 8 
> 
> 2) Segundo caso
> 
> $  perl -e  'use re 'debug'; $texto =~ s/program[ador,a,cion]/program/g;'
> Compiling REx `program[ador,a,cion]'
> size 13 first at 1
>    1: EXACT <program>(4)
>    4: ANYOF[,acdinor](13)
>   13: END(0)
> 
> Esto no es lo que quieres
> 
> 3) Tercer caso
> $ perl -e  'use re 'debug'; $texto =~ s/program[ador|a|cion]/program/g;'
> Compiling REx `program[ador|a|cion]'
> size 13 first at 1
>    1: EXACT <program>(4)
>    4: ANYOF[acdinor|](13)
>   13: END(0)
> 
> Esto tampoco.
> 
> Como ves [] simplemente define una clase de *caracteres*, igual que lo
> es \w o \d. Eso no te vale. Lo que necesitas es:
> 
> $ perl -e  'use re 'debug'; $texto =~ s/program(ador|a|acion)/program/g;'
> Compiling REx `program(ador|a|acion)'
> size 18 first at 1
>    1: EXACT <program>(4)
>    4: OPEN1(6)
>    6:   BRANCH(9)
>    7:     EXACT <ador>(16)
>    9:   BRANCH(12)
>   10:     EXACT <a>(16)
>   12:   BRANCH(16)
>   13:     EXACT <acion>(16)
>   16: CLOSE1(18)
>   18: END(0)
> 
> O si quieres coger tambi�n programadores, programatico, etc...:
> $ perl -e  'use re 'debug'; $texto =~ s/program[a-zA-Z]*?/program/g;' 
> Compiling REx `program[a-zA-Z]*?'
> size 15 first at 1
>    1: EXACT <program>(4)
>    4: MINMOD(5)
>    5: STAR(15)
>    6:   ANYOF[A-Za-z](0)
>   15: END(0)
> 
> Puedes echarle un vistazo tambi�n, por si tienes dudas a las p�ginas man
> perlre y perlretut.
> 
> Saludos
> 
> Samuel
Technical Details
Message-ID: <5b829932.0309020613.ab00a16@posting.google.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mail
References: <5b829932.0309011431.6fff8097@posting.google.com> <slrnbl8f1v.ks.444398NOSPAM@tizona.no-ip.org>

Show Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII
X-Google-Thread: f86c8,568a4d84eb300bba
X-Google-Attributes: gidf86c8,public
X-Google-ArrivalTime: 2003-09-02 07:13:06 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mail
From: francescomoi@europe.com (Francesco Moi)
Newsgroups: es.comp.os.linux.programacion
Subject: Re: Regexp & Perl: Como sustituir varias palabras parecidas
Date: 2 Sep 2003 07:13:02 -0700
Organization: http://groups.google.com/
Lines: 97
Message-ID: <5b829932.0309020613.ab00a16@posting.google.com>
References: <5b829932.0309011431.6fff8097@posting.google.com> <slrnbl8f1v.ks.444398NOSPAM@tizona.no-ip.org>
NNTP-Posting-Host: 212.8.114.52
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1062511986 5151 127.0.0.1 (2 Sep 2003 14:13:06 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: 2 Sep 2003 14:13:06 GMT