Completed firstMatch pattern function