title: regex last word in a sentence ending with punctuation (period)
body: <p>I'm looking for the regex pattern, not the Java code, to match the last word in an English (or European language) sentence.  If the last word is, in this case, "hi" then I want to match "hi" and not "hi."</p>
  
  <p>The regex <code>(\w+)\.$</code> will match "hi.", whereas the output should be just "hi".  What's the correct regex?</p>
  
  <pre><code>thufir@dur:~/NetBeansProjects/regex$ 
  thufir@dur:~/NetBeansProjects/regex$ java -jar dist/regex.jar 
  
  
  trying
  a b cd efg hi
  matches:
  hi
  
  
  trying
  a b cd efg hi.
  matches:
  thufir@dur:~/NetBeansProjects/regex$ 
  </code></pre>
  
  <p>code:</p>
  
  <pre><code>package regex;
  
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  public class Main {
  
      public static void main(String[] args) {
          String matchesLastWordFine = "a b cd efg hi";
          lastWord(matchesLastWordFine);
          String noMatchFound = matchesLastWordFine + ".";
          lastWord(noMatchFound);
      }
  
      private static void lastWord(String sentence) {
          System.out.println("\n\ntrying\n" + sentence + "\nmatches:");
          Pattern pattern = Pattern.compile("(\\w+)$");
          Matcher matcher = pattern.matcher(sentence);
          String match = null;
          while (matcher.find()) {
              match = matcher.group();
              System.out.println(match);
          }
      }
  }
  </code></pre>
  
  <p>My code is in Java, but that's neither here nor there.  I'm strictly looking for the regex, not the Java code.  (Yes, I know it's possible to strip out the last character with Java.)</p>
  
  <p>What regex should I put in the pattern?</p>