diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp
index b50d7328e47c..a84bc9abc4e9 100644
--- a/src/libcamera/media_device.cpp
+++ b/src/libcamera/media_device.cpp
@@ -305,39 +305,14 @@ bool MediaDevice::match(const DeviceMatch &device) const
 	if (driver_ != device.driver())
 		return false;
 
-	for (const std::string &name : device.entitiesNames()) {
-		bool found = false;
-
-		for (const MediaEntity *entity : entities()) {
-			if (name == entity->name()) {
-				if (!entity->deviceNode().empty()) {
-					found = true;
-					break;
-				} else {
-					LOG(MediaDevice, Debug)
-						<< "Skip " << entity->name()
-						<< ": no device node";
-				}
-			}
-		}
-
-		if (!found)
-			return false;
-	}
-
-	for (const std::regex &nameRegex : device.entitiesRegexs()) {
+	const auto searchEntities = [&](const auto &searchPattern,
+					const auto &searchFn, bool breakEarly) {
 		bool found = false;
 
 		for (const MediaEntity *entity : entities()) {
-			if (!std::regex_search(entity->name(), nameRegex))
+			if (!searchFn(searchPattern, entity->name()))
 				continue;
 
-			if (found) {
-				LOG(MediaDevice, Error)
-					<< "Multiple entities match regex";
-				return false;
-			}
-
 			if (entity->deviceNode().empty()) {
 				LOG(MediaDevice, Debug)
 					<< "Skip " << entity->name()
@@ -345,10 +320,34 @@ bool MediaDevice::match(const DeviceMatch &device) const
 				continue;
 			}
 
+			if (found) {
+				LOG(MediaDevice, Error)
+					<< "Multiple entities match "
+					<< entity->name();
+				return false;
+			}
+
 			found = true;
+			if (breakEarly)
+				break;
 		}
 
-		if (!found)
+		return found;
+	};
+
+	for (const std::string &name : device.entitiesNames()) {
+		if (!searchEntities(name, [](const std::string &s,
+					     const std::string &e) {
+			    return s == e;
+		    }, true))
+			return false;
+	}
+
+	for (const std::regex &nameRegex : device.entitiesRegexs()) {
+		if (!searchEntities(nameRegex, [](const std::regex &r,
+						  const std::string &e) {
+			    return std::regex_search(e, r);
+		    }, false))
 			return false;
 	}
 
