简体   繁体   中英

Pattern (string) allows characters only one time

I want to check if my string contains only allowed characters. Everything works properly for example 7B , 77B or 7BBBB , but when I input something like this 7B7 or 7BB2 it's not matching.

Everything work fine, but when integer is last character it's not working.

Could You tell me what is wrong with that code?

pattern = Pattern.compile("[0-9]*[a-f]*[A-F]*");
matcher = pattern.matcher(stNumber);
if (matcher.matches()) {...}

如果要以各种顺序混合数字和字符,则需要:

Pattern pattern = Pattern.compile("[\\da-fA-F]*")

Why not try it this way?

// Compile this pattern.
Pattern pattern = Pattern.compile("[0-9]*[a-f]*[A-F]*[0-9]*");

// See if this String matches.
Matcher m = pattern.matcher("num123");
if (m.matches()) {
    System.out.println(true);
}

Source

Are you trying to verify that the string only has digits and letters and nothing else?

If so try using the following:

pattern = Pattern.compile("^[a-z-A-Z\\d]*$");
matcher = pattern.matcher(stNumber);
if (matcher.matches()) {...}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM