For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2006 > substitute/regex etc









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author substitute/regex etc
Dan

2006-05-27, 6:58 pm

how can i substitute, or remove anything that's not an alphanumerical
character in a string?

i.e:
$string = "AbCdEF1246Hfn \n";
$string =~ s/[^a-z][^A-Z][^0-9]//g;

though to me, that would say:
(replace anything that's not a lower case letter as the first char) then
(replace anything that's not an upper case letter as the second char) then
(rpelace anything that's not a number as the third char).

or is this right?

thanks, dan


Charles K. Clarkson

2006-05-27, 6:58 pm

Dan wrote:
: how can i substitute, or remove anything that's not an
: alphanumerical character in a string?

my $string = "AbCdEF1246Hfn \n";

$string =~ tr/A-Za-z0-9//cd;

print qq{"$string"};


You can read more about tr/// in the "perlop" file in the docs.


: i.e:
: $string = "AbCdEF1246Hfn \n";
: $string =~ s/[^a-z][^A-Z][^0-9]//g;
:
: though to me, that would say:
: (replace anything that's not a lower case letter as the first
: char) then (replace anything that's not an upper case letter
: as the second char) then (rpelace anything that's not a number
: as the third char).
:
: or is this right?

It says replace any three character sequences where the first
character is not lower case, the second character is not upper case
and the final character is not a digit.


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
Free Market Advocate
Web Programmer

254 968-8328

Don't tread on my bandwidth. Trim your posts.

John W. Krahn

2006-05-27, 6:58 pm

Dan wrote:
> how can i substitute, or remove anything that's not an alphanumerical
> character in a string?


s/[[:alnum:]]+//g;


John
--
use Perl;
program
fulfillment
John W. Krahn

2006-05-27, 6:58 pm

John W. Krahn wrote:
> Dan wrote:
>
> s/[[:alnum:]]+//g;


Sorry, that should be:

s/[^[:alnum:]]+//g;


John
--
use Perl;
program
fulfillment
axel@white-eagle.invalid.uk

2006-05-28, 7:58 am

Dan <dan@danneh.org> wrote:
> how can i substitute, or remove anything that's not an alphanumerical
> character in a string?


> i.e:
> $string = "AbCdEF1246Hfn \n";
> $string =~ s/[^a-z][^A-Z][^0-9]//g;


> though to me, that would say:
> (replace anything that's not a lower case letter as the first char) then
> (replace anything that's not an upper case letter as the second char) then
> (rpelace anything that's not a number as the third char).


Yes.

If you wish to delete all alphanumeric characters using a character
class, try:

$string =~ s/[^A-Za-z0-9_]//g # An underscore is normally
# treated as alphanumeric

But far simpler to use:

$string =~ s/\W//g

With \W matching non-word characters (\w matches word characters).

You should read up on this in:

perdoc prelre

Axel
Dan

2006-05-29, 6:59 pm

""John W. Krahn"" <krahnj@telus.net> wrote in message
news:4478C6D0.1030102@telus.net...
> John W. Krahn wrote:
>
> Sorry, that should be:
>
> s/[^[:alnum:]]+//g;
>
>
> John
> --
> use Perl;
> program
> fulfillment


ohh clever! ;) that's much easier!
thanks!

dan


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com