Regex to include alphanumeric and _

前端 未结 2 1995
隐瞒了意图╮
隐瞒了意图╮ 2021-01-23 06:30

I\'m trying to create a regular expression to match alphanumeric characters and the underscore _. This is my regex: \"\\w_*[^-$\\s\\]\" and my impressi

相关标签:
2条回答
  • 2021-01-23 07:19

    Yes, you are semi-correct if the closing bracket was not escaped and you edited your regex a bit. Also the token \w matches underscore, so you do not need to repeat this character. Your regular expression says:

    \w         # word characters (a-z, A-Z, 0-9, _)
    _*         # '_' (0 or more times)
    [^-$\s]    # any character except: '-', '$', whitespace (\n, \r, \t, \f, and " ")
    

    You could simply write your entire regex as follows to match word characters:

    \w+        # word characters ( a-z, A-Z, 0-9, _ ) (1 or more times)
    

    If you want to match an entire string, be sure to anchor your expression.

    ^\w+$
    

    Explanation:

    ^          # the beginning of the string
     \w+       #   word characters ( a-z, A-Z, 0-9, _ ) (1 or more times)
    $          # before an optional \n, and the end of the string
    
    0 讨论(0)
  • 2021-01-23 07:22

    Regular expressions are read as patterns which actually match characters in a string, left to right, so your pattern actually matches an alphanumeric, THEN an underscore (0 or more), THEN at least one character that is not a hyphen, dollar, or whitespace.

    Since you're trying to alternate on character types, just use a character class to show what characters you're allowing:

    [\w_]
    

    This checks that ANY part of the string matches it, so let's anchor it to the beginning and and of the string:

    ^[\w_]$
    

    And now we see that the character class lacks a quantifier, so we are matching on exactly ONE character. We can fix that using + (if you want one or more characters, no empty strings) or * (if you want to allow empty strings). I'll use + here.

    ^[\w_]+$
    

    As it turns out, the \w character class already includes the underscore, so we can remove the redundant underscore from the pattern:

    ^[\w]+$
    

    And now we have only one character in the character class, so we no longer need the character class brackets at all:

    ^\w+$
    

    And that's all you need, unless I'm missing something about your requirements.

    0 讨论(0)
提交回复
热议问题