regular expression with alphanumeric with all special chars except with % - in the words.
it is 3 word separated with hyphen. first and second word can allow anything except % - Third word is only digits
first word
- not be empty (i.e. not a simple white space). but it can allow spaces in between.
- can not contain % and -
- length must be 1 to 10
second word
- not be empty (i.e. not a simple white space). but it can allow spaces in between
- can not contain % and -
- length must be 1 to 12
third word
- not be empty (i.e. not a simple white space)
- only digits allowed
- length must be 4 to 9
trying this way (as regex), but this is allowing % also.
[[^%\-\s]]{1,10}[-]{1}[[^%\-\s]]{1,12}[-]{1}[\d]{4,9}
allowed strings:
"1sAS-c$AS-01238"
"t t-c:t-012345678"
"t$t-c 2:t-012345678"
Not allowed strings:
"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)
example my input string (itemValue) for above pattern, should give false: test%t1z
Pattern pattern = Pattern.compile(regexStr);
Matcher matcher = pattern.matcher(itemValue);
if(matcher.matches()){
flag = true;
}