Regular Expression for alphanumeric and underscores

前端 未结 20 972
北荒
北荒 2020-11-22 10:01

I would like to have a regular expression that checks if a string contains only upper and lowercase letters, numbers, and underscores.

20条回答
  •  萌比男神i
    2020-11-22 10:39

    The following regex matches alphanumeric characters and underscore:

    ^[a-zA-Z0-9_]+$
    

    For example, in Perl:

    #!/usr/bin/perl -w
    
    my $arg1 = $ARGV[0];
    
    # check that the string contains *only* one or more alphanumeric chars or underscores
    if ($arg1 !~ /^[a-zA-Z0-9_]+$/) {
      print "Failed.\n";
    } else {
        print "Success.\n";
    }
    

提交回复
热议问题