[wplug] isAlpha
James O'Kane
jo2y at midnightlinux.com
Wed Aug 16 05:06:46 EDT 2006
On Wed, 16 Aug 2006, James LaSalle wrote:
> I want to extract all the text characters from a string and filter out all
> the non alphabetic characters using a Perl script. Is there a Perl function
> to do this or can someone have a Perl snippet to point the way? At this point
> it looks like I must code something with a regex substitute structure.
I haven't tested it, but I think you want something like this:
$foo =~ s/[^A-Za-z]//g;
[] creates a class of characters.
The A-Za-z lists all the letters of the alphabet. You can add more at the
end of the list.
The ^ as the first character of the class negates the group.
So you have any character that is not a letter of the alphabet.
The s///g does a search and replace changing the first group into the
second, in this case, nothing. The /g makes it global to the whole string,
so it will repeat until it doesn't match anymore.
If you have long strings of non-alphabetic characters, this might be
faster:
$foo =~ s/[^A-Za-z]+//g;
-james
More information about the wplug
mailing list