【发布时间】:2021-08-08 21:31:37
【问题描述】:
带有字母数字和所有特殊字符的正则表达式,除了 % - 在单词中。
它是用连字符分隔的 3 个单词。第一个和第二个单词可以允许除 % - 第三个词只有数字
第一个词
- 不能为空(即不是简单的空白)。但它可以允许中间有空格。
- 不能包含 % 和 -
- 长度必须是 1 到 10
第二个字
- 不能为空(即不是简单的空白)。但它可以允许中间有空格
- 不能包含 % 和 -
- 长度必须是 1 到 12
第三个字
- 不能为空(即不是简单的空白)
- 只允许数字
- 长度必须为 4 到 9
尝试这种方式(作为正则表达式),但这也允许 %。
[[^%\-\s]]{1,10}[-]{1}[[^%\-\s]]{1,12}[-]{1}[\d]{4,9}
允许的字符串:
"1sAS-c$AS-01238"
"t t-c:t-012345678"
"t$t-c 2:t-012345678"
不允许的字符串:
"1sA%S-c$AS-01238" (because this contains % in the first word)
" -c:t-01235678" (because this contains only space in the first word)
"t$t- -01234578" (because this contains only space in the 2nd word)
"t-$t-check-012378" (because this contains more hypens)
上述模式的输入字符串(itemValue)示例,应为 false:test%t1z
Pattern pattern = Pattern.compile(regexStr);
Matcher matcher = pattern.matcher(itemValue);
if(matcher.matches()){
flag = true;
}
【问题讨论】: