You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Resolve inconsistency between \w and \b determination for what is a word character #62619
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.
usingSystem;usingSystem.Linq;usingSystem.Text;usingSystem.Text.RegularExpressions;//Console.WriteLine(typeof(string).Assembly.Location);vartext="ZERO\u200cWIDTH\u200cJOINER\nNON\u200DZERO\u200DWIDTH\u200cJOINER";varmatches=Regex.Matches(text,@"\b(\w+)\b");foreach(Matchminmatches){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(Matchminmatches){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
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?
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.
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?
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.
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.
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)
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.
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.
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).
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.
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]?
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?
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.
Activity
joperezr commentedon Jul 18, 2022
@stephentoub do we still want to do this for 7.0?
danmoseley commentedon Jul 18, 2022
TR18 is probably less important then what PCRE, Perl etc do. I can check.
stephentoub commentedon Jul 18, 2022
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.
danmoseley commentedon Jul 18, 2022
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)danmoseley commentedon Jul 18, 2022
eg
gives
on both .NET 6 and .NET 7 and .NET Framework
What am I missing?
danmoseley commentedon Jul 18, 2022
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?
stephentoub commentedon Jul 18, 2022
Semantically, no.
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.
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:
is
falsebecause\u200cis not a word character, this:is also
false, because even thoughzis 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.
danmoseley commentedon Jul 18, 2022
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\bin 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.stephentoub commentedon Jul 18, 2022
Presumably because that's how someone interpreted the remarks in TR18.
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.
You could probably simulate something with lookarounds.
Because the first match consumes the
\u200c?\bis zero-width;\u200cis not.danmoseley commentedon Jul 18, 2022
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.
veanes commentedon Jul 19, 2022
Let me add my take on the situation, I think I may have been the one raising the
\wvs\bissue originally.Here is the kind of situation that is unique to .NET. The below regex
rseems fairly common I think.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 (
\u200cis considered a word char) or 6.(
\u200cis considered a non-word char)stephentoub commentedon Jul 19, 2022
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
danmoseley commentedon Jul 19, 2022
@joperezr any preference? Makes the code a little easier to reason about as well.
joperezr commentedon Jul 19, 2022
I agree that we shouldn't do special-casing for those two characters, as it just causes inconsistencies between meanings of
\wand\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.veanes commentedon Jul 20, 2022
Note that the easier path is to treat
\u200cand\u200das non-word characters --- just because then we don't have to regenerate the serialized representation of\w, alternatively we can add\u200cand\u200dinto the BDD created from the serialized representation each time (not too difficult either but would be a small overhead each time\wis being used for the first time), or else we regenerate the serialized representation so these two are included from the start.stephentoub commentedon Jul 20, 2022
ICU includes \u200c and \u200d in the definition of \w:
https://unicode-org.github.io/icu/userguide/strings/regexp.html
and TR18 states:
http://www.unicode.org/reports/tr18/
Java also includes it:
https://regex101.com/r/OBF3hj/1
as does Rust:

https://rustexp.lpil.uk/
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:
I'll put up a PR.
stephentoub commentedon Jul 20, 2022
Actually, this direction is harder than I expected:
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]?
joperezr commentedon Jul 20, 2022
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?stephentoub commentedon Jul 22, 2022
Right. \w is exactly the combination of:
UppercaseLetterLowercaseLetterTitlecaseLetterModifierLetterOtherLetterNonSpacingMarkDecimalDigitNumberConnectorPunctuation\u200cand\u200dare instead in theFormatcategory, which today contains 38 other characters as well, so we can't just addFormatto the list.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.