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
Apparently the empty string neither is nor isn't a word boundary. Is that supposed to happen? \B matches the empty string in every other language I can think of.
# However, an empty string contains no word boundaries, and also no
# non-boundaries.
self.assertIsNone(re.search(r"\B", ""))
Strictly speaking, re is working as designed here, even if that design is arguably "incorrect" with respect to other regex implementations.
Changing this behavior would technically be a feature request (not a bug), and would likely require a deprecation period. I'd recommend opening a thread on https://discuss.python.org/c/ideas/6 to see if there's community interest in changing this.
For reference, JavaScript treats the empty string as containing a non-word boundary:
# However, an empty string contains no word boundaries, and also no
# non-boundaries.
self.assertIsNone(re.search(r"\B", ""))
# This one is questionable and different from the perlre behaviour,
# but describes current behavior.
self.assertIsNone(re.search(r"\b", ""))
However the comments really confuse me since the behavior for \b atm is same as Perl's but it's still claiming different from the perlre behaviour. Or have I missed some changes on the behavior of \b later on? If that was the case, I think it's also reasonable to align the behavior of \B to Perl's.
I think both comments apply to the \B case, as that is the one different from Perl. Splitting the comment that way is a bit confusing if one does not know Perl, which most do not now. @serhiy-storchaka can comment, but I don't think we can really change this now.
This test was added in 5a045b9 (bpo-10713/gh-54922). It was not an assertion for the intended behavior, it was added to ensure that the current behavior would not change. Strictly speaking, the current behavior contradicts the documentation that says that \B is the opposite of \b.
Of course, it may be that the documentation is wrong. But taking into account that the current behavior differs from the behavior of many (if not all) other RE engines, that the code is most likely a copying error (it was not properly tested), that we already did several breaking changes related to zero-width matches in the past, and that it affects only very specific cases, I think that we can and should change this behavior.
It is preferable to emit a FutureWarning first, but I do not know how difficult to do this without producing false positives. If it is too difficult or impossible, we will have no other way as to change the behavior without warning.
Reacted by Brian Schubert, Gregory P. Smith, Shantanu and Alex Waygood
We've been reluctant to make any changes to the re module behavior in the past as people's existing code depends on all of its behaviors per Hyrum's Law.
matching occurrence:
\b "" \B ""
python 0 0
OTHERS 0 1
\b "a" \B "a"
ALL 2 0
\b "=" \B "="
ALL 0 2
Searching \B on GitHub did not find valuable information.
It would be great if Regex101 website (the link above this comment) could provide some examples of how users using \B.
Activity
brianschubert commentedon Sep 16, 2024
The current behavior (
\Bnot matching"") appears to be intentional:cpython/Lib/test/test_re.py
Lines 896 to 898 in aba42c0
Strictly speaking,
reis working as designed here, even if that design is arguably "incorrect" with respect to other regex implementations.Changing this behavior would technically be a feature request (not a bug), and would likely require a deprecation period. I'd recommend opening a thread on https://discuss.python.org/c/ideas/6 to see if there's community interest in changing this.
For reference, JavaScript treats the empty string as containing a non-word boundary:
y5c4l3 commentedon Sep 16, 2024
Another Perl reference:
$ perl -E 'say "Boundary" if "" =~ /\b/; say "Non-boundary" if "" =~ /\B/;' Non-boundarySeems that it is taking Perl as a baseline to some extent, as indicated in
test_re:cpython/Lib/test/test_re.py
Lines 896 to 901 in 9017b95
However the comments really confuse me since the behavior for
\batm is same as Perl's but it's still claimingdifferent from the perlre behaviour. Or have I missed some changes on the behavior of\blater on? If that was the case, I think it's also reasonable to align the behavior of\Bto Perl's.terryjreedy commentedon Sep 16, 2024
I think both comments apply to the
\Bcase, as that is the one different from Perl. Splitting the comment that way is a bit confusing if one does not know Perl, which most do not now. @serhiy-storchaka can comment, but I don't think we can really change this now.serhiy-storchaka commentedon Sep 17, 2024
This test was added in 5a045b9 (bpo-10713/gh-54922). It was not an assertion for the intended behavior, it was added to ensure that the current behavior would not change. Strictly speaking, the current behavior contradicts the documentation that says that
\Bis the opposite of\b.Of course, it may be that the documentation is wrong. But taking into account that the current behavior differs from the behavior of many (if not all) other RE engines, that the code is most likely a copying error (it was not properly tested), that we already did several breaking changes related to zero-width matches in the past, and that it affects only very specific cases, I think that we can and should change this behavior.
It is preferable to emit a FutureWarning first, but I do not know how difficult to do this without producing false positives. If it is too difficult or impossible, we will have no other way as to change the behavior without warning.
gpshead commentedon Sep 17, 2024
We've been reluctant to make any changes to the
remodule behavior in the past as people's existing code depends on all of its behaviors per Hyrum's Law.vstinner commentedon Sep 20, 2024
The best that we can do is to document the bug, if there is a bug, and explain that it's kept for backward compatibility.
ZeroIntensity commentedon Sep 21, 2024
The linked PR is now just a documentation note.
gh-124130: Notes on empty string corner case of category `\B` (#124133)
pythongh-124130: Notes on empty string corner case of category `\B` (p…
6 remaining items
[3.13] gh-124130: Notes on empty string corner case of category `\B` (G…
gh-124130: Increase test coverage for \b and \B in regular expressions (
pythongh-124130: Increase test coverage for \b and \B in regular expr…
pythongh-124130: Increase test coverage for \b and \B in regular expr…
[3.12] gh-124130: Increase test coverage for \b and \B in regular exp…
[3.13] gh-124130: Increase test coverage for \b and \B in regular exp…
wjssz commentedon Nov 19, 2024
I tested the recent version of:
\Bcan't match""only in Python:Searching
\Bon GitHub did not find valuable information.It would be great if Regex101 website (the link above this comment) could provide some examples of how users using
\B.pythongh-124130: Fix a bug in matching regular expression \B in empty…
serhiy-storchaka commentedon Nov 19, 2024
#127007 changes the behavior of
\Bfor an empty string.gh-124130: Fix a bug in matching regular expression \B in empty string (
pythongh-124130: Fix a bug in matching regular expression \B in empty…