I need Java regular expression to match String which contains lowercase or uppercase or 1 or more hyphens and may end in period(.) comma(,) question mark(?) exclamation mark(!)
example: done-done, Done!, where?, Ahhh! are some of the inputs that should satisfy the regex
I came up with "^([a-z][A-Z]) ! (.|,|\\?|!)?$")
but not working as expected, appreciate anyone can help me with this.
CodePudding user response:
See if this regex helps you:
^[a-zA-Z\-] [\.,!?]?$
If you need to accept an empty string as well, you should use a *
:
^[a-zA-Z\-]*[\.,!?]?$
Bear in mind that the above regex will also accept strings that only contain . , ! or ?
.