Regex: How to match a string that is not only numbers

后端 未结 11 736
夕颜
夕颜 2020-11-30 03:18

Is it possible to write a regular expression that matches all strings that does not only contain numbers? If we have these strings:

  • abc
相关标签:
11条回答
  • 2020-11-30 03:42

    I had to do something similar in MySQL and the following whilst over simplified seems to have worked for me:

    where fieldname regexp ^[a-zA-Z0-9]+$ and fieldname NOT REGEXP ^[0-9]+$

    This shows all fields that are alphabetical and alphanumeric but any fields that are just numeric are hidden. This seems to work.

    example:

    name1 - Displayed
    name - Displayed
    name2 - Displayed
    name3 - Displayed
    name4 - Displayed
    n4ame - Displayed
    324234234 - Not Displayed

    0 讨论(0)
  • 2020-11-30 03:45

    jjnguy had it correct (if slightly redundant) in an earlier revision.

    .*?[^0-9].*
    

    @Chad, your regex,

    \b.*[a-zA-Z]+.*\b
    

    should probably allow for non letters (eg, punctuation) even though Svish's examples didn't include one. Svish's primary requirement was: not all be digits.

    \b.*[^0-9]+.*\b
    

    Then, you don't need the + in there since all you need is to guarantee 1 non-digit is in there (more might be in there as covered by the .* on the ends).

    \b.*[^0-9].*\b
    

    Next, you can do away with the \b on either end since these are unnecessary constraints (invoking reference to alphanum and _).

    .*[^0-9].*
    

    Finally, note that this last regex shows that the problem can be solved with just the basics, those basics which have existed for decades (eg, no need for the look-ahead feature). In English, the question was logically equivalent to simply asking that 1 counter-example character be found within a string.

    We can test this regex in a browser by copying the following into the location bar, replacing the string "6576576i7567" with whatever you want to test.

    javascript:alert(new String("6576576i7567").match(".*[^0-9].*"));
    
    0 讨论(0)
  • 2020-11-30 03:46
    .*[^0-9]{1,}.*
    

    Works fine for us.

    We want to use the used answer, but it's not working within YANG model.

    And the one I provided here is easy to understand and it's clear: start and end could be any chars, but, but there must be at least one NON NUMERICAL characters, which is greatest.

    0 讨论(0)
  • 2020-11-30 03:53
    /^\d*[a-z][a-z\d]*$/
    

    May be a digit at the beginning, then at least one letter, then letters or digits

    0 讨论(0)
  • 2020-11-30 03:54

    If we want to restrict valid characters so that string can be made from a limited set of characters, try this:

    (?!^\d+$)^[a-zA-Z0-9_-]{3,}$
    

    or

    (?!^\d+$)^[\w-]{3,}$
    

    /\w+/: Matches any letter, number or underscore. any word character

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