I\'d like to test if a string ends with a a digit. I expect the following Java line to print true. Why does it print false?
System.out.println(\"I end with
In Java Regex, there's a difference between Matcher.find() (find a match anywhere in the String) and Matcher.matches() (match the entire String).
String only has a matches()
method (implemented equivalent to this code:Pattern.compile(pattern).matcher(this).matches();
), so you need to create a pattern that matches the full String:
System.out.println("I end with a number 4".matches("^.*\\d$"));