java - How to test if a String contains both letter and number

前端 未结 7 1197
轻奢々
轻奢々 2020-12-16 01:07

I need a regex which will satisfy both conditions.

It should give me true only when a String contains both A-Z and 0-9.

Here\'s what I\'ve tried:

相关标签:
7条回答
  • 2020-12-16 01:09

    A letter may be either before or after the digit, so this expression should work:

    (([A-Z].*[0-9])|([0-9].*[A-Z]))
    

    Here is a code example that uses this expression:

    Pattern p = Pattern.compile("(([A-Z].*[0-9])|([0-9].*[A-Z]))");
    Matcher m = p.matcher("AXD123");
    boolean b = m.find();
    System.out.println(b);
    
    0 讨论(0)
  • 2020-12-16 01:14

    Try using (([A-Z]+[0-9])|([0-9]+[A-Z])) .It should solve.

    0 讨论(0)
  • 2020-12-16 01:16

    What about ([A-Z].*[0-9]+)|([0-9].*[A-Z]+) ?

    0 讨论(0)
  • 2020-12-16 01:24

    I suspect that the regex below is slowed down by the look-around, but it should work regardless:

    .matches("^(?=.*[A-Z])(?=.*[0-9])[A-Z0-9]+$")
    

    The regex asserts that there is an uppercase alphabetical character (?=.*[A-Z]) somewhere in the string, and asserts that there is a digit (?=.*[0-9]) somewhere in the string, and then it checks whether everything is either alphabetical character or digit.

    0 讨论(0)
  • 2020-12-16 01:26

    This should solve your problem:

    ^([A-Z]+[0-9][A-Z0-9]*)|([0-9]+[A-Z][A-Z0-9]*)$
    

    But it's unreadable. I would suggest to first check input with "^[A-Z0-9]+$", then check with "[A-Z]" to ensure it contains at least one letter then check with "[0-9]" to ensure it contains at least one digit. This way you can add new restrictions easily and code will remain readable.

    0 讨论(0)
  • 2020-12-16 01:32

    Here is the regex for you

    Basics:

    Match in the current line of string: .

    Match 0 or any amount of any characters: *

    Match anything in the current line: .*

    Match any character in the set (range) of characters: [start-end]

    Match one of the regex from a group: (regex1|regex2|regex3)

    Note that the start and end comes from ASCII order and the start must be before end. For example you can do [0-Z], but not [Z-0]. Here is the ASCII chart for your reference

    Check the string against regex

    Simply call yourString.matches(theRegexAsString)

    Check if string contains letters:

    Check if there is a letter: yourString.matches(".*[a-zA-Z].*")

    Check if there is a lower cased letter: yourString.matches(".*[a-z].*")

    Check if there is a upper cased letter: yourString.matches(".*[A-Z].*")

    Check if string contains numbers:

    yourString.matches(".*[0-9].*")

    Check if string contains both number and letter:

    The simplest way is to match twice with letters and numbers

    yourString.matches(".*[a-zA-Z].*") && yourString.matches(".*[0-9].*")

    If you prefer to match everything all together, the regex will be something like: Match a string which at someplace has a character and then there is a number afterwards in any position, or the other way around. So your regex will be:

    yourString.matches(".*([a-zA-Z].*[0-9]|[0-9].*[a-zA-Z]).*")

    Extra regex for your reference:

    Check if the string stars with letter

    yourString.matches("[a-zA-Z].*")

    Check if the string ends with number

    yourString.matches(".*[0-9]")

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