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 a number 4".matches("\\d$"));
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In Java Regex, there’s a difference between
Matcher.find()(find a match anywhere in the String) andMatcher.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: