Fun fact. A word
is a single distinct element of speech or writing to form a sentence and typically shown with a space on either side. \w
matches (any letter
, number
or underscore
)
It is unclear to exactly what you are asking without a better explanation of what you are trying to accomplish.
If you want to match a word that contains letters
and apostrophe '
with more than 3
characters..
List words = new ArrayList();
String s = "I want to have alot of money's when I am older.";
Pattern p = Pattern.compile("[a-zA-Z']{4,}");
Matcher m = p.matcher(s);
while (m.find()) {
words.add(m.group());
}
System.out.println(words);
// [want, have, alot, money's, when, older]
Note: This matches a word that contain's more than 3
characters, if you also want to match a word that contains 3
characters (foo
) or more, you can use the following.
Pattern p = Pattern.compile("[a-zA-Z']{3,}");