I have a requirement to build a regex pattern to validate a String in Java. Hence I build a pattern
[A-Z][a-z]*\s?[A-Z]?[a-z]*$ for the conditions:
- Should start with caps
- Every other Word should start with caps
- No numbers included
- no consecutive two spaces allowed
Pattern.matches("[A-Z][a-z]*\s?[A-Z]?[a-z]*$","Joe V") returns false for me in java.
But the same pattern returns true for the data "Joe V" in regexr.com.
What might be the cause?
s?, it seems you expect that to match a space..., but that would need to be\s?, and in a string literal with escaped backslash...