Title: Question about adjacent empty matches in regular expressions · Issue #122055 · python/cpython · GitHub
Open Graph Title: Question about adjacent empty matches in regular expressions · Issue #122055 · python/cpython
X Title: Question about adjacent empty matches in regular expressions · Issue #122055 · python/cpython
Description: Documentation The Python documentation for re.sub() states: Empty matches for the pattern are replaced only when not adjacent to a previous empty match. However, after some testing, I have been unable to construct a regular expression pa...
Open Graph Description: Documentation The Python documentation for re.sub() states: Empty matches for the pattern are replaced only when not adjacent to a previous empty match. However, after some testing, I have been una...
X Description: Documentation The Python documentation for re.sub() states: Empty matches for the pattern are replaced only when not adjacent to a previous empty match. However, after some testing, I have been una...
Opengraph URL: https://github.com/python/cpython/issues/122055
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Question about adjacent empty matches in regular expressions","articleBody":"# Documentation\r\n\r\nThe Python [documentation](https://docs.python.org/3/library/re.html#re.sub) for re.sub() states:\r\n\r\n\u003e Empty matches for the pattern are replaced only when not adjacent to a previous empty match.\r\n\r\nHowever, after some testing, I have been unable to construct a regular expression pattern that produces adjacent empty matches. This leads to the following questions:\r\n\r\nIs it actually possible to create a regular expression pattern that results in adjacent empty matches in Python's re module?\r\nIf not, should we consider updating the documentation to avoid potential confusion among developers?\r\n\r\n## My Investigation\r\n\r\nI've tried various patterns that might theoretically produce adjacent empty matches, such as:\r\n\r\n```python\r\nimport re\r\n\r\ndef find_all_matches(pattern, string):\r\n return [m.start() for m in re.finditer(pattern, string)]\r\n\r\ntest_string = \"abcd\"\r\npatterns = [\r\n r'\\b|\\b', # Word boundaries\r\n r'^|$', # Start or end of string\r\n r'(?=.)|(?\u003c=.)', # Positive lookahead or lookbehind\r\n r'.*?', # Non-greedy any character\r\n]\r\n\r\nfor pattern in patterns:\r\n matches = find_all_matches(pattern, test_string)\r\n print(f\"Pattern {pattern}: {matches}\")\r\n```\r\n\r\nNone of these patterns produce adjacent empty matches. The regex engine seems to always move forward after finding a match, even an empty one.\r\n\r\n## Request for Clarification\r\n\r\nThis issue sincerely requests clarification on this matter:\r\n\r\n1. If adjacent empty matches are indeed possible as the documentation suggests, could you provide some examples that demonstrate this behavior? \r\n\r\n2. Are there specific scenarios or edge cases where adjacent empty matches can occur?\r\n\r\n3. If possible, could you share a minimal working example that shows how `re.sub()` handles adjacent empty matches differently from non-adjacent ones?\r\n\r\nThese examples would greatly help in understanding the documentation and the behavior of the `re` module in such cases.\r\n\r\nThank you for your time and attention to this matter. Any insights or examples you can provide would be greatly appreciated.\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-133169\n* gh-134217\n* gh-134218\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/krave1986","@type":"Person","name":"krave1986"},"datePublished":"2024-07-20T11:25:39.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":8},"url":"https://github.com/122055/cpython/issues/122055"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:85149f99-f9f6-d1f9-5bad-399bea075635 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DB46:1BDC7:513982:6FA74E:6A4DCA1A |
| html-safe-nonce | 2a4d55e1a65f10f2ca5626ffb0921e9dba16eff35e035e992ec81608e2ea58af |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQjQ2OjFCREM3OjUxMzk4Mjo2RkE3NEU6NkE0RENBMUEiLCJ2aXNpdG9yX2lkIjoiMjA4OTAwNTYyOTkzODk3NzMwNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 922bac6d56d73ff2f599977d2d3885d9f3916fee3c5a83a4400cd0da3132083a |
| hovercard-subject-tag | issue:2420855985 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/122055/issue_layout |
| twitter:image | https://opengraph.githubassets.com/23003b59c6ed4689f6dbf66d56c1e3ce51b71557deaa100d2630d69506fdaa2e/python/cpython/issues/122055 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/23003b59c6ed4689f6dbf66d56c1e3ce51b71557deaa100d2630d69506fdaa2e/python/cpython/issues/122055 |
| og:image:alt | Documentation The Python documentation for re.sub() states: Empty matches for the pattern are replaced only when not adjacent to a previous empty match. However, after some testing, I have been una... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | krave1986 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 06b8a6144231bf3a234f1c2e9993861e07ce98a905912b114aa386c2d7e84b33 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 1d344bdb7547fe6bca17a59bb2b8aac3dc9532a0 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width