### Elasticsearch Version

`main`

### Installed Plugins

_No response_

### Java Version

_bundled_

### OS Version

`Linux kore 6.1.25-1-MANJARO #1 SMP PREEMPT_DYNAMIC Thu Apr 20 13:48:36 UTC 2023 x86_64 GNU/Linux`

### Problem Description

While working on #95666, I encountered a test failure because the following warning message was emitted by `MetadataIndexTemplateService`:


```
legacy template [global] has index patterns [*] matching patterns from existing composable templates [.deprecation-indexing-template,.fleet-file-data,.fleet-files,.ml-anomalies-,.ml-notifications-000002,.ml-state,.ml-stats,.monitoring-beats-mb,.monitoring-ent-search-mb,.monitoring-es-mb,.monitoring-kibana-mb,.monitoring-logstash-mb,.profiling-ilm-lock,.slm-history,.watch-history-16,behavioral_analytics-events-default,ilm-history,logs,metrics,profiling-events,profiling-executables,profiling-metrics,profiling-returnpads-private,profiling-sq-executables,profiling-sq-leafframes,profiling-stackframes,profiling-stacktraces,profiling-symbols,synthetics] with patterns (.deprecation-indexing-template => [.logs-deprecation.*],.fleet-file-data => [.fleet-file-data-*-*],.fleet-files => [.fleet-files-*-*],.ml-anomalies- => [.ml-anomalies-*],.ml-notifications-000002 => [.ml-notifications-000002],.ml-state => [.ml-state*],.ml-stats => [.ml-stats-*],.monitoring-beats-mb => [.monitoring-beats-8-*],.monitoring-ent-search-mb => [.monitoring-ent-search-8-*],.monitoring-es-mb => [.monitoring-es-8-*],.monitoring-kibana-mb => [.monitoring-kibana-8-*],.monitoring-logstash-mb => [.monitoring-logstash-8-*],.profiling-ilm-lock => [.profiling-ilm-lock*],.slm-history => [.slm-history-5*],.watch-history-16 => [.watcher-history-16*],behavioral_analytics-events-default => [behavioral_analytics-events-*],ilm-history => [ilm-history-5*],logs => [logs-*-*],metrics => [metrics-*-*],profiling-events => [profiling-events*],profiling-executables => [profiling-executables*],profiling-metrics => [profiling-metrics*],profiling-returnpads-private => [.profiling-returnpads-private*],profiling-sq-executables => [.profiling-sq-executables*],profiling-sq-leafframes => [.profiling-sq-leafframes*],profiling-stackframes => [profiling-stackframes*],profiling-stacktraces => [profiling-stacktraces*],profiling-symbols => [.profiling-symbols*],synthetics => [synthetics-*-*]); this template [global] may be ignored in favor of a composable template at index creation time
```

This happened here:


https://github.com/elastic/elasticsearch/blob/86f37160ae3c24c4dcaaf0b41637136275a9fdca/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java#L1059-L1060

and attempting to emit a warning header lead to:

```
» [2023-05-10T07:36:15,606][ERROR][o.e.b.ElasticsearchUncaughtExceptionHandler] [v8.9.0-remote-0] fatal error in thread [elasticsearch[v8.9.0-remote-0][masterService#updateTask][T#1]], exiting java.lang.StackOverflowError
»       at java.base/java.util.regex.Pattern$GroupHead.match(Pattern.java:4828)
»       at java.base/java.util.regex.Pattern$Loop.match(Pattern.java:4937)
»       at java.base/java.util.regex.Pattern$GroupTail.match(Pattern.java:4859)
»       at java.base/java.util.regex.Pattern$BranchConn.match(Pattern.java:4737)
»       at java.base/java.util.regex.Pattern$BmpCharProperty.match(Pattern.java:3993)
»       at java.base/java.util.regex.Pattern$Branch.match(Pattern.java:4773)
»       at java.base/java.util.regex.Pattern$GroupHead.match(Pattern.java:4828)
»       at java.base/java.util.regex.Pattern$Loop.match(Pattern.java:4937)
»       at java.base/java.util.regex.Pattern$GroupTail.match(Pattern.java:4859)
[...]
```

The `StackOverflowError` is caused by the following assertion:

https://github.com/elastic/elasticsearch/blob/86f37160ae3c24c4dcaaf0b41637136275a9fdca/server/src/main/java/org/elasticsearch/common/logging/HeaderWarning.java#L333

(the following line is probably also problematic)

### Steps to Reproduce

This is a test case that reproduces the issue:

```java
public class HeaderWarningTests extends ESTestCase {
    public void testAddComplexWarning() {
        final int maxWarningHeaderCount = 2;
        Settings settings = Settings.builder().put("http.max_warning_header_count", maxWarningHeaderCount).build();
        ThreadContext threadContext = new ThreadContext(settings);
        final Set<ThreadContext> threadContexts = Collections.singleton(threadContext);
        HeaderWarning.addWarning(
            threadContexts,
            "legacy template [global] has index patterns [*] matching patterns from existing composable templates " +
                "[.deprecation-indexing-template,.fleet-file-data,.fleet-files,.ml-anomalies-,.ml-notifications-000002,.ml-state," +
                ".ml-stats,.monitoring-beats-mb,.monitoring-ent-search-mb,.monitoring-es-mb,.monitoring-kibana-mb," +
                ".monitoring-logstash-mb,.profiling-ilm-lock,.slm-history,.watch-history-16,behavioral_analytics-events-default," +
                "ilm-history,logs,metrics,profiling-events,profiling-executables,profiling-metrics,profiling-returnpads-private," +
                "profiling-sq-executables,profiling-sq-leafframes,profiling-stackframes,profiling-stacktraces," +
                "profiling-symbols,synthetics] with patterns (.deprecation-indexing-template => [.logs-deprecation.*]," +
                ".fleet-file-data => [.fleet-file-data-*-*],.fleet-files => [.fleet-files-*-*],.ml-anomalies- => [.ml-anomalies-*]," +
                ".ml-notifications-000002 => [.ml-notifications-000002],.ml-state => [.ml-state*],.ml-stats => [.ml-stats-*]," +
                ".monitoring-beats-mb => [.monitoring-beats-8-*],.monitoring-ent-search-mb => [.monitoring-ent-search-8-*]," +
                ".monitoring-es-mb => [.monitoring-es-8-*],.monitoring-kibana-mb => [.monitoring-kibana-8-*]," +
                ".monitoring-logstash-mb => [.monitoring-logstash-8-*],.profiling-ilm-lock => [.profiling-ilm-lock*]," +
                ".slm-history => [.slm-history-5*],.watch-history-16 => [.watcher-history-16*]," +
                "behavioral_analytics-events-default => [behavioral_analytics-events-*],ilm-history => [ilm-history-5*]," +
                "logs => [logs-*-*],metrics => [metrics-*-*],profiling-events => [profiling-events*],profiling-executables => " +
                "[profiling-executables*],profiling-metrics => [profiling-metrics*],profiling-returnpads-private => " +
                "[.profiling-returnpads-private*],profiling-sq-executables => [.profiling-sq-executables*]," +
                "profiling-sq-leafframes => [.profiling-sq-leafframes*],profiling-stackframes => [profiling-stackframes*]," +
                "profiling-stacktraces => [profiling-stacktraces*],profiling-symbols => [.profiling-symbols*],synthetics => " +
                "[synthetics-*-*]); this template [global] may be ignored in favor of a composable template at index creation time"
        );
        final Map<String, List<String>> responseHeaders = threadContext.getResponseHeaders();

        assertThat(responseHeaders.size(), equalTo(1));
        final List<String> responses = responseHeaders.get("Warning");
        assertThat(responses, hasSize(1));
        assertThat(responses.get(0), warningValueMatcher);
        assertThat(responses.get(0), containsString("\"legacy template [global] has index patterns"));
        assertThat(responses.get(0), containsString(Integer.toString(299)));
    }
}
```

### Logs (if relevant)

_No response_
server/src/main/java/org/elasticsearch/common/logging/HeaderWarning.java
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */
package org.elasticsearch.common.logging;
import org.elasticsearch.Build;
import org.elasticsearch.Version;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.tasks.Task;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.BitSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
 * This is a simplistic logger that adds warning messages to HTTP headers.
 * Use <code>HeaderWarning.addWarning(message,params)</code>. Message will be formatted according to RFC7234.
 * The result will be returned as HTTP response headers.
 */
public class HeaderWarning {