I am trying to have Perl count only the non-special characters (as in what one can find on a US keyboard: numbers letters, %$#()
, etc.)
my $stg = "é§I¥3§";
For the string above, I have tried several examples from threads such as these: Use Perl to check if a string has only English characters
my $char_count = () = $stg =~ /[[:alpha:]]/;
my $char_count = () = $stg =~ /[a-zA-Z0-9_]/;
my $char_count = () = $stg =~ /[^a-zA-z0-9_]*$/;
All of them give me 1 when the answer I am looking for is: 2 (only 'I' and '3' counted). Can anyone tell me what the correct solution is?
Transliteration is another method for counting. With the /c
flag, it uses the complement of the characters you specify:
my $non_ascii_count = $stg =~ tr/a-zA-Z0-9_//c;
You always get 1 because your regular expression stops after the 1st match. You will get 2 if you add the //g modifier ("Match globally") to your last 2 regexes:
my $char_count = () = $stg =~ /[a-zA-Z0-9_]/g;
my $char_count = () = $stg =~ /[^a-zA-z0-9_]*$/g;
The :alpha: regex only matches I
, not 3
. It will return 1 even with //g
:
alpha Any alphabetical character (e.g., [A-Za-z]).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.