Name Pattern Example
Example Requirements
- First name with minimum 2 word characters & maximum 20 characters
- Middle name initial only
- Last name with minimum 2 word characters & maximum 25 characters
Simply regex code
1 2 3 4 5 6 7 |
/* * Name Pattern Example */ String FIRST_M_LAST_NAME_REGEX = regex().startingWith().between(2, 20) .occurrencesOf(charThatIs().anyWordChar().build()).then().anyWhiteSpaceChar().then().exact(1) .occurrencesOf(charThatIs().anyWordChar().build()).then().anyWhiteSpaceChar().then().between(2, 25) .occurrencesOf(charThatIs().anyWordChar().build()).then().endOfText().build(); |
Output of java.util.regex.Matcher.matches()
“dwayne D johnson” –> true
“Dwayne D Johnson” –> true
“Dwayne D Jjjjjjjjjjjjjjjjjjjjjjjjj” –> true
“D D Johnson” –> false
“Dwayne DD Johnson” –> false
“Dwayne D Jjjjjjjjjjjjjjjjjjjjjjjjjj” –> false
“Dwayne d” –> false
“Dwayne D Johnson zzz” –> false
“!@!@Dwayne D Johnson” –> false