Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment