Reputation: 1231
I need to extract words with apostrophe from text file. I have a program that extract words that their length are certain amount(in the following program 3) and have apostrophe. However I need the one to consider only and only those words with apostrophe without considering any other conditions.
public static void main(String[] args) throws IOException {
ArrayList<String> words = new ArrayList<String>();
String s = "I want to have a lot of money's when I am older.";
Pattern p = Pattern.compile("[a-zA-Z']{3,}");
Matcher m = p.matcher(s);
while (m.find()) {
words.add(m.group());
}
System.out.println(words);
Upvotes: 0
Views: 431
Reputation: 59113
This pattern:
"[a-zA-Z']*'[a-zA-Z']*"
matches any sequence of letters/apostrophes, containing at least one apostrophe.
Therefore it will match even such things as:
Upvotes: 0
Reputation: 832
But you know this of the word:
Pattern p = Pattern.compile("\\w*'\\w*");
Upvotes: 4
Reputation: 4987
Try this:
Pattern p = Pattern.compile("[a-zA-Z]{2,}'[a-zA-Z]*");
With this regex you match words with two or more letters, and apostrophe and optional letter after the apostrophe. You can change the minimum/maximum letters in [a-zA-Z]{2,}
and in the last part insted of asterisk you can use {min,max}
to specify precisely
Upvotes: 1