exclude underscore from alpha numeric regex

前端 未结 5 1582
轮回少年
轮回少年 2020-12-10 11:47

I want use \\w regex for to allow alpha numeric but I don\'t want underscore _ to be part of it. Since _ is included in \\w

5条回答
  •  悲哀的现实
    2020-12-10 12:24

    Assuming the identifier must begin with an alpha character, and then may contain any number of alpha or numeric, I would do this:

    my $string = 'roger54a';
    print "Match\n" if $string =~ m/\A\p{alpha}[\p{alpha}\p{Number}]*\z/;
    

    That anchors to the start and end of the string, precluding any characters that don't match the specific set of a single alpha followed by any quantity of alpha and numerics.

    Update: I see tchrist just gave a great explanation of the Unicode properties. This answer provides the context of a full regexp.

    If you wanted the leading 'alphas' to be two or three digits followed by alpha-numeric, just add the appropriate quantifier:

    $string =~ m/\A\p{alpha}{2,3}[\p{alpha}\p{Number}]*\z/

    Update2: I see a stronger definition of what you're looking for in a comment to one of the answers here. Here's my take on it after seeing your clarification:

    m/\Aroger[\p{alpha}\p{Number}]{2,3}\z/

提交回复
热议问题