View Javadoc
1   package lv.id.jc.piglatin.core;
2   
3   import java.util.function.Function;
4   import java.util.regex.Matcher;
5   import java.util.regex.Pattern;
6   
7   import org.springframework.stereotype.Component;
8   
9   /**
10   * This class is responsible for matching words in a given phrase.
11   */
12  @Component
13  public class WordsMatcher implements Function<String, Matcher> {
14      private static final Pattern WORD = Pattern.compile("""
15              # A word contains only Unicode letters and apostrophe (') in the middle
16              (               # Start of the group
17                  \\p{L}      # Any Unicode letter
18              |               # OR
19                  (?<=\\p{L}) # Positive lookbehind for a Unicode letter
20                  '           # An apostrophe
21                  (?=\\p{L})  # Positive lookahead for a Unicode letter
22              )+              # One or more of the previous group
23              """,
24          Pattern.COMMENTS + Pattern.CASE_INSENSITIVE);
25  
26      /**
27       * This method applies the word-matching pattern to a given phrase.
28       * It returns a Matcher object that can be used to find words in the phrase.
29       *
30       * @param phrase the phrase to apply the word-matching pattern to
31       * @return a Matcher object for finding words in the phrase
32       */
33      @Override
34      public Matcher apply(String phrase) {
35          return WORD.matcher(phrase);
36      }
37  }