The Perl subroutine below returns a true value if its input is a valid C variable name, and false otherwise. This uses two regular expressions, one to check for C keywords, and the other to check the characters in the variable's name.
#!/usr/local/bin/perl use warnings; use strict; my @reserved_words = sort {length $b <=> length $a} qw/auto if break int case long char register continue return default short do sizeof double static else struct entry switch extern typedef float union for unsigned goto while enum void const signed volatile/; my $reserved_words_re = join '|', @reserved_words; # Returns an undefined value unless $variable_name is a valid C # variable. sub valid_c_variable { my ($variable_name) = @_; if ($variable_name !~ /^[A-Za-z_][A-Za-z_0-9]+$/ || $variable_name =~ /^(?:$reserved_words_re)$/) { return; } return 1; }