-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-124130: Fix a bug in matching regular expression \B in empty string #127007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
serhiy-storchaka
merged 4 commits into
python:main
from
serhiy-storchaka:re-non-boundary
Jan 2, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
592a769
gh-124130: Fix a bug in matching regular expression \B in empty string
serhiy-storchaka bbace33
Update docs.
serhiy-storchaka 4c79573
Merge branch 'main' into re-non-boundary
serhiy-storchaka ef0a6fc
Merge branch 'main' into re-non-boundary
serhiy-storchaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Library/2024-11-19-10-46-57.gh-issue-124130.OZ_vR5.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Fix a bug in matching regular expression ``\B`` in empty input string. | ||
Now it is always the opposite of ``\b``. | ||
To get an old behavior, use ``(?!\A\Z)\B``. | ||
To get a new behavior in old Python versions, use ``(?!\b)``. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix is LGTM.
Some users may not be sure what "the whole empty string" is.
I suggest explaining in more detail, and no need to use "whole" for "empty string".
\B
used to be unable to match empty string. Now it can match, this behavior is consistent with mainstream languages.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know why you use "whole", the
\B
description:You want to distinguish from "empty string" here.
Let me think about how to describe it more clearly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is still difficult to avoid contradiction with "\B Matches the empty string, but only when it ...".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, "the empty string" means two different things there. The docs you're removing don't do a very good job either. Should be disambiguated somehow, for example
\B now matches if the input string is empty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this:
\B used to be unable to match 0-length string
""
/b""
. Now it can match, this behavior is consistent with mainstream languages.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Alcaro uses the word "input", it looks fine. Candidate:
\B
now matches empty input string""
/b""
, this behavior is consistent with RE implementations in mainstream programming languages.The previous test: #124130 (comment)
After that, I tested ASCII mode for "a", Unicode mode for "Ρ".
The results are same
except in Perl v5.40.0.Tested with: regex module, openjdk, .net, rust, ruby, php, pcre2.
javascript/golang only support ASCII word (for \w\W\b\B), even in Unicode mode.
It seems Perl v5.40.0 has bug in (\B
+ Unicode_mode).Given this, it's not recommended to mention Perl in doc.
Save it to file
perl.pl
, and run:perl perl.pl
Click to see Perl script
Script output:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, Perl doesn't have (\B+Unicode_mode) bug.
If add a line
use utf8;
at the beginning of Perl script, it works as expected.ref: https://perldoc.perl.org/utf8