简体   繁体   English

REGEX az 0-9但不仅仅是数字

[英]REGEX a-z 0-9 but not only numbers

Trying to match only strings 10-30 characters long with only az and 0-9 (but not only numbers) in the string. 尝试仅匹配10-30个字符长的字符串,仅匹配字符串中的az和0-9(但不仅仅是数字)。 Seems to work Except when the string starts with a number then it fails. 似乎工作除了字符串以数字开头然后失败。 Not sure about the \\D which should fix the not only numbers 不确定\\ D应该修复不仅数字

static final Pattern UNIQUE_ID_PATTERN = Pattern.compile("^\\D[A-Za-z0-9_-]{10,30}$");
UNIQUE_ID_PATTERN.matcher("1eeeeeeeee333e").matches(); // Does not work
UNIQUE_ID_PATTERN.matcher("eeeeeeeee333e").matches(); // Works

The \\D shorthand class means any non-digit symbol . \\D简写类表示any non-digit symbol You should remove it from the pattern (so that it becomes "^[A-Za-z0-9_-]{10,30}$" ) for the matches to return true, as 1 is a digit in 1eeeeeeeee333e . 您应该从模式中删除它(以便它变为"^[A-Za-z0-9_-]{10,30}$" )以使matches返回true,因为11eeeeeeeee333e的数字。

If you want to place a restriction (the string cannot consist of only digits) use an anchored look-ahead: 如果要设置限制(字符串不能只包含数字),请使用锚定前瞻:

^(?![0-9]+$)[A-Za-z0-9_-]{10,30}$

Here is a demo 这是一个演示

Or, a shortened version with i modifier making the pattern case-insensitive: 或者,带有i修饰符的缩短版本使模式不区分大小写:

(?i)^(?![0-9]+$)[A-Z0-9_-]{10,30}$

Another way: ^(?=(?:.*[a-zA-Z])+)([a-zA-Z\\d]{10,30})$ update 另一种方式: ^(?=(?:.*[a-zA-Z])+)([a-zA-Z\\d]{10,30})$ update

Demo 演示

More details 更多细节

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Regex az,AZ,0-9和(。)(_)( - ) - Java Regex a-z, A-Z, 0-9 and (.)(_)(-) 正则表达式包含AZ 0-9和/或空格,括号或连字符 - Regex to contain A-Z 0-9 and/or spaces, parenthesis or hyphen 包含除 [AZ][az][0-9][\\s][-] 以外的任何字符串的正则表达式 - Regex for string containing anything other than [A-Z][a-z][0-9][\s][-] 如何仅使用Java从文本文件中读取字母(az || AZ)和数字(0-9)字符? - How to read alphabets (a-z || A-Z) and digits (0-9) characters from a text file only in Java? 如何制作一个正则表达式来匹配以 0-9 或 az 开头的带有重音符号的字符串,并且必须只接受这个特殊字符 - 单词之间的 _ '? - How to make a regex to match string that starts with 0-9 or a-z with accents and must accept only this special character - _ ' between words? 如何使用正则表达式在望头中给出范围,例如^(?=(。* [az]){1,3})(?=。* [0-9])。{2,5} $ - How to give range in lookhead using regex e.g ^(?=(.*[a-z]){1,3})(?=.*[0-9]).{2,5}$ 正则表达式:用连字符替换空格,只允许使用az - Regex: replace whitespaces with hyphens and allow only a-z 为什么带有正则表达式 ^[az]$ 的 find() 不等于带有正则表达式 [az] 的 match()? - Why is find() with Regex ^[a-z]$ not equivalent to matches() with Regex [a-z]? 正则表达式删除到t / aZ / - Regex to remove to t/a-Z/ 替换字符串az 0-9和旁边的所有字符, - Replace all characters in string beside a-z 0-9 and ,
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM