Regex: only alphanumeric but not if this is pure numeric

后端 未结 7 1951
北荒
北荒 2020-11-29 11:34

For instance:

\'1\'     => NG
\'243\'   => NG
\'1av\'   => OK
\'pRo\'   => OK
\'123k%\' => NG

I tried with

          


        
相关标签:
7条回答
  • 2020-11-29 11:59

    Try with this:

    /^(?!^\d*$)[a-zA-Z\d]*$/
    

    Edit: since this is essentially the same of the accepted answer, I'll give you something different:

    /^\d*[a-zA-Z][a-zA-Z\d]*$/
    

    No lookaheads, no repeated complex group, it just verifies that there's at least one alphabetic character. This should be quite fast.

    0 讨论(0)
  • 2020-11-29 12:03

    Try this: ^[a-zA-Z0-9]*[a-zA-Z]+[a-zA-Z0-9]*$

    0 讨论(0)
  • 2020-11-29 12:04

    try with this: ^(?!\d+\b)[a-zA-Z\d]+$
    (?!\d+\b) will avoid pure numeric, add \w+ then mix alphabet and number.

    0 讨论(0)
  • 2020-11-29 12:05

    This should do the trick, without lookbehind:

    ^(\d*[a-zA-Z]\d*)+$
    
    0 讨论(0)
  • 2020-11-29 12:15

    So we know that there must be at least one "alphabetic" character in there somewhere:

    [a-zA-Z]
    

    And it can have any number of alphanumeric characters (including zero) either before it or after it, so we pad it with [a-zA-Z0-9]* on both sides:

    /^[a-zA-Z0-9]*[a-zA-Z][a-zA-Z0-9]*$/
    

    That should do the trick.

    0 讨论(0)
  • 2020-11-29 12:15

    this is strictly alphanumeric

    String name="^[^a-zA-Z]\\d*[a-zA-Z][a-zA-Z\\d]*$";
    
    0 讨论(0)
提交回复
热议问题