Skip to content

Resolve inconsistency between \w and \b determination for what is a word character #62619

Closed as not planned

Description

@stephentoub

Currently our definition of \b explicitly adds \u200c and \u200d beyond what \w represents. The TR18 report calls out that these should be part of word boundaries, but there's ambiguity as to whether they should also be part of \w. We appear to have made an explicit choice at some point to not include these two additional chars in the definition of \w, but that seems like a strange inconsistency, one which we bend over backwards to maintain in a variety of places. We should reconsider whether this discrepancy continues to make sense, or whether it would make sense for these to also be considered a match for \w.

cc: @veanes

Activity

  1. added this to the 7.0.0 milestone on Dec 10, 2021
  2. ghost added
    untriaged
    on Dec 10, 2021
  3. removed
    untriaged
    on Jan 11, 2022
  4. joperezr commented on Jul 18, 2022

    @joperezr
    Member

    @stephentoub do we still want to do this for 7.0?

  5. danmoseley commented on Jul 18, 2022

    @danmoseley
    Contributor

    TR18 is probably less important then what PCRE, Perl etc do. I can check.

  6. stephentoub commented on Jul 18, 2022

    @stephentoub
    MemberAuthor

    The work here is minimal if we decide to make a change. We just need to make a call. Ideally we make that call for .NET 7.

  7. danmoseley commented on Jul 18, 2022

    @danmoseley
    Contributor

    On .NET 6 and .NET 7 preview 5, neither of them match \b -- see https://regex101.com/r/gPrCln/1 -- this is at odds with what you're noting above.
    In PCRE, Python, Golang, Java, both match \b.

    In all cases neither of them match \w (all match \W)

  8. danmoseley commented on Jul 18, 2022

    @danmoseley
    Contributor

    eg

    using System;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    //Console.WriteLine(typeof(string).Assembly.Location);
    var text = "ZERO\u200cWIDTH\u200cJOINER\nNON\u200DZERO\u200DWIDTH\u200cJOINER";
    
    var matches = Regex.Matches(text, @"\b(\w+)\b");
    
    foreach(Match m in matches)
    {
        Console.WriteLine($"Word with \\b on each side: {m.Value}");
    }
    
    Console.WriteLine($"Total words with \\b on each side: {matches.Count}");
    
     matches = Regex.Matches(text, @"(\w+)");
    
    foreach(Match m in matches)
    {
        Console.WriteLine($"Word: {m.Value}");
    }
    
    Console.WriteLine($"Total words: {matches.Count}");

    gives

    Total words with \b on each side: 0
    Word: ZERO
    Word: WIDTH
    Word: JOINER
    Word: NON
    Word: ZERO
    Word: WIDTH
    Word: JOINER
    Total words: 7
    

    on both .NET 6 and .NET 7 and .NET Framework

    What am I missing?

  9. danmoseley commented on Jul 18, 2022

    @danmoseley
    Contributor

    I think perhaps what was intended in the top comment was, "While these do not match \w, we specially treat them so they do match \w for the purposes of \b, so they will never match \b, which is defined as the boundary in (^\w|\w$|\W\w|\w\W). However other engines do consider these to not match \w for the purposes of \b"

    Did anything change in .NET 7 here? It seems on the face of it the same back to .NET Framework. Although odd, any particular reason to fix today?

  10. stephentoub commented on Jul 18, 2022

    @stephentoub
    MemberAuthor

    Did anything change in .NET 7 here?

    Semantically, no.

    Although odd, any particular reason to fix today?

    We have a new engine in .NET 7 that has to emulate the difference. It'd be nice it that wasn't the case.

    I think we either address this in 7 or decide it is what it is and close this.

    What am I missing?

    I'm not sure what your repro is trying to show.

    \w matches a set of characters. When we ask "is this character a word character", we see if the character is in that set. But when we ask "is this character considered a word character for the purpose of deciding whether it's at a word boundary", we see whether it's in that set or is \u200c or \u200d. So even though:

    Regex.IsMatch("\u200c", @"\w")

    is false because \u200c is not a word character, this:

    Regex.IsMatch("\u200cz‌\u200c", @"\b\w\b")

    is also false, because even though z is a word character and it's surrounded by two characters (\u200c) that aren't word characters, they're considered word characters for the purposes of the anchor analysis.

    As https://regex101.com/r/nrhRqT/1 shows, PCRE, JavaScript, Python, Golang, and Java all do something different from what .NET does here.

  11. danmoseley commented on Jul 18, 2022

    @danmoseley
    Contributor

    Right, I think that's what my repro shows too.

    If I understand correctly, we have no knowledge of why it was done this way, nor how many if any people depend on it. If someone really wanted the current behavior, is there a clear workaround?

    Eg., if I replace \b\w\b in your example with (?:\b|\u200c)\w(?:\b|\u200c) https://regex101.com/r/L1gW3p/1 I'm only getting a match on a, and not b. Java / Python give both a and \u200cb. I can't immediately see why.

  12. stephentoub commented on Jul 18, 2022

    @stephentoub
    MemberAuthor

    we have no knowledge of why it was done this way

    Presumably because that's how someone interpreted the remarks in TR18.

    nor how many if any people depend on it

    Yes, we don't have a good way to know this, nor how the opposite of how many false negatives/positives devs are erroneously getting as a result without realizing it.

    If someone really wanted the current behavior, is there a clear workaround?

    You could probably simulate something with lookarounds.

    I can't immediately see why.

    Because the first match consumes the \u200c? \b is zero-width; \u200c is not.

  13. danmoseley commented on Jul 18, 2022

    @danmoseley
    Contributor

    Oh! Of course.

    Across Regex we have generally biased towards what seems to be most useful, and any consensus behavior of other engines (mainly PCRE), rather than specs. That would be consistent with making the change.

  14. veanes commented on Jul 19, 2022

    @veanes
    Contributor

    Let me add my take on the situation, I think I may have been the one raising the \w vs \b issue originally.
    Here is the kind of situation that is unique to .NET. The below regex r seems fairly common I think.

    Regex r = new Regex(@"\b\w+\b");
    var m = r.Matches("n\u200co wor\u200cds he\u200cre");
    Console.WriteLine(m.Count);
    

    Will print 0, that takes quite a while to figure out why, and is definitely counterintuitive to say the least.
    All other matchers, as far as I know, either print 3 (\u200c is considered a word char) or 6.
    (\u200c is considered a non-word char)

  15. stephentoub commented on Jul 19, 2022

    @stephentoub
    MemberAuthor

    The change is small if we want to do it:
    main...stephentoub:runtime:slashbslashwconsistency
    The removes the \u200c and \u200d special-casing from \b such that it considers the identical set as \w. The other direction is also possible, e.g. make \w also consider \u200c and \u200d as word characters, which appears to be Java's interpretation of TR18 when you opt-in to full unicode matching:
    https://regex101.com/r/vonZDn/1

  16. danmoseley commented on Jul 19, 2022

    @danmoseley
    Contributor

    @joperezr any preference? Makes the code a little easier to reason about as well.

  17. joperezr commented on Jul 19, 2022

    @joperezr
    Member

    I agree that we shouldn't do special-casing for those two characters, as it just causes inconsistencies between meanings of \w and \b. My only pushback would be if people depended on this inconsistency, but sounds like we don't think that's the case, so I'm in favor of making the change.

  18. veanes commented on Jul 20, 2022

    @veanes
    Contributor

    Note that the easier path is to treat \u200c and \u200d as non-word characters --- just because then we don't have to regenerate the serialized representation of \w, alternatively we can add \u200c and \u200d into the BDD created from the serialized representation each time (not too difficult either but would be a small overhead each time \w is being used for the first time), or else we regenerate the serialized representation so these two are included from the start.

  19. stephentoub commented on Jul 20, 2022

    @stephentoub
    MemberAuthor

    ICU includes \u200c and \u200d in the definition of \w:
    https://unicode-org.github.io/icu/userguide/strings/regexp.html

    Word characters are [\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\u200c\u200d].
    

    and TR18 states:
    http://www.unicode.org/reports/tr18/

    To meet this requirement, an implementation shall extend the word boundary mechanism so that:
    The class of <word_character> includes all the Alphabetic values from the Unicode character database, from [UnicodeData.txt](https://www.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt), plus the decimals (General_Category=Decimal_Number, or equivalently Numeric_Type=Decimal), and the U+200C ZERO WIDTH NON-JOINER and U+200D ZERO WIDTH JOINER (Join_Control=True). See also [Annex C: Compatibility Properties](http://www.unicode.org/reports/tr18/#Compatibility_Properties).
    

    Java also includes it:
    https://regex101.com/r/OBF3hj/1

    as does Rust:
    https://rustexp.lpil.uk/
    image
    https://github.com/rust-lang/regex/blob/9ca3099037dcb2faf1b49e6493f4c758532f2da1/regex-syntax/src/unicode.rs#L322-L338
    https://github.com/rust-lang/regex/blob/c31428ad59c1f9b13a1cc6dac0ac48e50eff512c/regex-syntax/src/unicode_tables/perl_word.rs#L307
    Note as well this from Rust's Unicode.md:

    In particular, this differs slightly from the [prescription given in RL1.4](https://unicode.org/reports/tr18/#Simple_Word_Boundaries) but is permissible according to [UTS#18 Annex C](https://unicode.org/reports/tr18/#Compatibility_Properties). Namely, it is convenient and simpler to have \w and \b be in sync with one another.
    

    I'll put up a PR.

  20. self-assigned this
    on Jul 20, 2022
  21. stephentoub commented on Jul 20, 2022

    @stephentoub
    MemberAuthor

    I'll put up a PR.

    Actually, this direction is harder than I expected:

    • Making \b not include the two additional characters, easy.
    • Making \w include the two additional characters, hard.

    The problem is actually with how we represent character classes internally. The representation is essentially a set of ranges for characters to include, a set of Unicode categories to include, and an optional negation bit. Importantly, the representation of the categories also allows them to be defined as exclusions rather than as inclusions. So \w is defined today as a set of several Unicode category inclusions, no ranges, no negation, and \W is defined today as a set of several Unicode category exclusions, no ranges, no negation. If \w needs to include the two additional characters, no problem, we just add in the ranges for them. But what about \W? The main way we could represent that would be the same as the new \w but with the negation bit set, however that then falls over when it's combined into a larger character class, e.g. how do you represent [\Wab]?

  22. joperezr commented on Jul 20, 2022

    @joperezr
    Member

    I see, it is indeed rather complicated. And I suppose there is no Unicode Category that is already imported into \w that could just take these extra two characters so as to not require ranges to define it?

    Given this problem, should we just go with the other solution for this inconsistency that you quoted above: The removes the \u200c and \u200d special-casing from \b such that it considers the identical set as \w?

  23. stephentoub commented on Jul 22, 2022

    @stephentoub
    MemberAuthor

    And I suppose there is no Unicode Category that is already imported into \w that could just take these extra two characters so as to not require ranges to define it?

    Right. \w is exactly the combination of:

    • UppercaseLetter
    • LowercaseLetter
    • TitlecaseLetter
    • ModifierLetter
    • OtherLetter
    • NonSpacingMark
    • DecimalDigitNumber
    • ConnectorPunctuation

    \u200c and \u200d are instead in the Format category, which today contains 38 other characters as well, so we can't just add Format to the list.

    The removes the \u200c and \u200d special-casing from \b such that it considers the identical set as \w?

    I've gone back and forth, but I think if we're going to make a change, we should do it "right", and I think "right" would be making both of these word characters. I don't see a good path to doing so for the foreseeable future, though. Given that, I think the least bad option is to just stick with what we currently have.

    As such, I'll close this. If we ever get negative feedback about it, and/or if we ever change our set representation to better enable this, we can revisit.

  24. ghost locked as resolved and limited conversation to collaborators on Aug 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions