[Solved] java.lang.IllegalStateException: No match found while using Regex Named Groups

You will get this exception message when you are trying to use named groups in java regex validation or matching.

Root Cause of No match found Error in Regex Named Group Matching

Error log will show the message like below:

Exception in thread "main" java.lang.IllegalStateException: No match found
	at java.util.regex.Matcher.group(Unknown Source)


You are probably not using “matcher.matches()” before fetching the named group from matcher. You should do the complete oprtation like below:

List<String> dates = new ArrayList<String>();
dates.add("02/31/2011");  //Invalid date
dates.add("02/27/2011");  //Valid date

String regex = "^(?<month>[0-3]?[0-9])/(?<day>[0-3]?[0-9])/(?<year>(?:[0-9]{2})?[0-9]{2})$";

Pattern pattern = Pattern.compile(regex);

for (String date : dates)
	Matcher matcher = pattern.matcher(date);

	//This is the root cause of error. Don't forget to do this !!

	//Get date parts here
	String day = matcher.group(2);
	String month = matcher.group(1);
	String year = matcher.group(3);

	String formattedDate = month + "/" + day + "/" + year;

	System.out.println("Date to check : " + formattedDate);

This will solve your error in most of the cases.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

2 thoughts on “[Solved] java.lang.IllegalStateException: No match found while using Regex Named Groups”

    matcher.matches(); // for regexes like "^...(?<targetGroup>regex)...$"
    matcher.lookingAt(); // for regexes like "...(?<targetGroup>regex)..."

    Implementation for this kind of cases smells like non transparent behavior with object mutations inside the methods.
    matcher.matches() and matcher.lookingAt() returnbool state – gorgeous


Leave a Comment


A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.