| Author |
substitute/regex etc
|
|
|
| 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
| |
|
| ""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
|
|
|
|