-1

I use a regular expression to match words (and not match embedded strings) that looks looks like this:

word.trim().matches("(\\w+)(\\.|\\?|\\!|\\<)?");

This works great if the word is a string like this word = " blue ";

But it fails if the word has an embedded &, or any special character, like this
word = " A&P "; or this word = " A-P ";

How can I alter my regular expression so that it picks up certain special characters like "&" and "-"

Elliott
  • 5,211
  • 8
  • 43
  • 80

1 Answers1

1

This seems about the simplest:

str.matches("(?i)[a-z]+([-&][a-z]+)?[.!?<]?")

Also note the re-phrasing of your alternation to a character class, since each term was a single character and being a character class you don't even need to escape dot or "?".

The (?i) means "ignore case".

I'm not confident about the "<" being a legitimate "word ending"; you might consider removing it from the regex.

Bohemian
  • 365,064
  • 84
  • 522
  • 658