Skip to content

<regex>: How should multi-character collating elements in character classes be handled? #6552

Backlog

Description

As mentioned in #6540, the loop subtype for negated character classes in collate mode is missing because such character classes aren't marked as simple, because there is a reasonable set of semantics where such character classes could match character sequences of varying length if there are multi-character collating elements. (Specifically, this will be a consequence of option 4 below.) The question is: What are the appropriate semantics we should implement?

Unfortunately, the POSIX standard, which originally added collating elements to regular expressions, isn't of much help in the version referenced by the C++ standard (if interested, expand below).

POSIX (un-)specification

The POSIX standard states:

  • For the list of expressions inside a positive character class:

It is unspecified whether a matching list expression matches a multi-character collating element that is matched by one of the expressions.

  • For the list of expressions inside a negated character class:

It is unspecified whether a non-matching list expression matches a multi-character collating element that is not matched by any of the expressions.

  • For named character classes (e.g., [:alpha:]):

An unspecified set of multi-character collating elements.

  • For character ranges (note that the C locale doesn't have multi-character collating elements):

In [locales other than the C locale], a range expression has unspecified behavior: strictly conforming applications shall not rely on whether the range expression is valid, or on the set of collating elements matched.

So nowadays, the POSIX standard leaves basically everything about multi-character collating elements in character classes unspecified and thus is of very little help.

In turn, the C++ standard effectively specifies at least some of this:

  • Given that isctype() in the traits class is only defined for matching single characters, named character classes can't match multi-character collating elements.
  • In [re.grammar], it defines how range expressions are to be interpreted in collate mode or non-collate mode, at least for single characters.

The current implementation in MSVC STL has at least one unfortunate inconsistency: It breaks the property that the combination of two positive character classes actually represents the union of the matched character sequences, so [c[.ch.]]h does not match everything that [c]h or [[.ch.]]h match (or, expressed differently, [c[.ch.]]h does not match the same sequences as ([c]|[[.ch.]])h).

Test case

#include <iostream>
#include <locale>
#include <regex>
#include <string>

using namespace std;

class custom_traits : public regex_traits<char> {
public:
    custom_traits() {
        regex_traits<char>::imbue(locale("cs_CZ"));
    }
    template <class FwdIt>
    string_type lookup_collatename(FwdIt first, FwdIt last) const {
        string_type name(first, last);
        if (name == "ch" || name == "Ch") {
            return name;
        }

        return regex_traits::lookup_collatename(first, last);
    }
};

int main() {
    for (string pattern : {"[c]h", "[[.ch.]]h", "[c[.ch.]]h"}) {
        basic_regex<char, custom_traits> re(pattern);
        cout << "pattern '" << pattern
                  << "' matches ch: " << std::regex_match("ch", re)
                  << " matches chh: " << std::regex_match("chh", re) << '\n';
    }

    return 0;
}

https://godbolt.org/z/qe1YTM63T

Equivalent test case for Boost:

#include <boost/regex.hpp>
#include <iostream>
#include <locale>
#include <string>

int main() {
    boost::regex re;
    re.imbue(std::locale("cs_CZ"));
    for (std::string pattern : {"[c]h", "[[.ch.]]h", "[c[.ch.]]h"}) {
        re.assign(pattern);
        std::cout << "pattern '" << pattern
                  << "' matches ch: " << boost::regex_match("ch", re)
                  << " matches chh: " << boost::regex_match("chh", re) << '\n';
    }

    return 0;
}

https://godbolt.org/z/bKEK5nz84

Results of the test case (x/y means: regex_match returns x on "ch" and y on "chh"):

Library [c]h [[.ch.]]h [c[.ch.]]h
MSVC STL 1/0 0/1 0/1
libstdc++ 1/0 1/0 (???) 1/0
libc++ 0/1 (???) 0/1 0/1
Boost.Regex 1/0 0/1 1/0
GNU grep 3.11 1/0 0/1 1/1

Disagreement is rampant. MSVC STL and Boost.Regex fail the property that the combination of two character classes should match the set union. The other three have that property, but libstdc++ and libc++ only have it due to obvious bugs as highlighted in the table above.

Options

  1. Keep things as-is, with the unfortunate consequence that [c]|[[.ch.]] does not match the same character sequences as [c[.ch.]].
  2. Align with Boost.Regex, so switch the order when multi-character collating elements are checked compared to single characters. The only advantage is that we behave like Boost.Regex now, but it's still the case that [c]|[[.ch.]] does not match the same character sequences as [c[.ch.]].
  3. Make [c[.ch.]] match c or ch (i.e., follow GNU grep). This will complicate the matcher: It must be taught to partially backtrack the matching of a character class (i.e., if matching "ch" for character class "[c[.ch.]]" succeeded, but the match failed later, then backtrack to the character class and retry with "c").
  4. Make [c]h not match ch, i.e., the character class must always match the longest collating element and not any prefix of it. This means that processing a character class will have to call lookup_collatename() first to find the collating element (by essentially doing lookup_collatename(cur, cur+x) == string(cur, cur + x)), then check that this collating element is in the character class. Problems:
    • There is no traits API to find the longest collating element that is a prefix of a character sequence, so we would have to impose an arbitrary maximum length that is checked using lookup_collatename(). 3 appears reasonable, though, because that is the length of the longest collating element in Hungarian. We could double-check using ICU, though, if there is a longer collating element in some language.
    • The additional lookup_collatename() calls make character matching more expensive.
    • This approach only seems reasonable when the collate flag is set: It results in behavior people do not usually expect, so we can only do this when users explicitly ask for behavior consistent with locale collation. So even if we go with this option, we still have to decide the appropriate semantics when collate is not set.
    • libc++ kind of tries to implement this, but the implementation is very buggy (see, e.g., boost::regex match but not std::regex llvm/llvm-project#39399).
    • I think this is closest to the way collating elements were supposed to behave when they were added to POSIX, but I'm not aware that any actively maintained regex engine properly implements this anymore.
    • For consistent behavior, a negated character class might also match multi-character collating elements when this option is implemented.

Activity

  1. added
    question
    on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions