user3147590
user3147590

Reputation: 1231

Extract only words with apostrophe from text file

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

Answers (3)

khelwood
khelwood

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:

  • O'Neill's in "Colonel O'Neill's rifle."
  • 'n' in "Fish 'n' chips"

Upvotes: 0

engineercoding
engineercoding

Reputation: 832

But you know this of the word:

  1. contains chars before the apostrophe
  2. apostrophe
  3. more char(s)

Pattern p = Pattern.compile("\\w*'\\w*");

Upvotes: 4

Kristijan Iliev
Kristijan Iliev

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

Related Questions