Pewarta Proxy

📋 Server Request & Proxy Logs

Loading active proxy settings...
Developed by Khairul Adha (adhexa.id)
Mengambil log server...

⚙️ Pengaturan Proxy Target

Skip to content

re module doesn't describe string boundaries for \b #54922

Closed

Description

@ralphcorderoy
BPO 10713
Nosy @ezio-melotti, @merwok
Files
  • 20110822-1604-re-docs.diff
  • issue10713.diff: Patch against 3.2.
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ezio-melotti'
    closed_at = <Date 2012-02-29.09:51:34.281>
    created_at = <Date 2010-12-16.01:05:33.040>
    labels = ['easy', 'type-feature', 'docs']
    title = "re module doesn't describe string boundaries for \\b"
    updated_at = <Date 2012-02-29.09:51:34.279>
    user = 'https://bugs.python.org/ralphcorderoy'

    bugs.python.org fields:

    activity = <Date 2012-02-29.09:51:34.279>
    actor = 'ezio.melotti'
    assignee = 'ezio.melotti'
    closed = True
    closed_date = <Date 2012-02-29.09:51:34.281>
    closer = 'ezio.melotti'
    components = ['Documentation']
    creation = <Date 2010-12-16.01:05:33.040>
    creator = 'ralph.corderoy'
    dependencies = []
    files = ['22991', '24661']
    hgrepos = []
    issue_num = 10713
    keywords = ['patch', 'easy']
    message_count = 8.0
    messages = ['124097', '135466', '135524', '142679', '154470', '154479', '154607', '154608']
    nosy_count = 7.0
    nosy_names = ['ezio.melotti', 'eric.araujo', 'ralph.corderoy', 'docs@python', 'python-dev', 'Ron.Ridley', 'poolie']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue10713'
    versions = ['Python 2.7', 'Python 3.2', 'Python 3.3']

    Activity

    1. ralphcorderoy commented on Dec 16, 2010

      ralphcorderoymannequin
      MannequinAuthor

      The re module defines \b in a regexp to need \w one side and \W the other. What about when the end of the string or line is involved? perlre(1) says that's treated as a \W. Python should precisely document that case too.

    2. merwok commented on May 7, 2011

      @merwok
      Member

      Thanks for the report. Would you be interested in experimenting and/or reading the code to find the anwser and propose a doc patch?

    3. ralphcorderoy commented on May 8, 2011

      ralphcorderoymannequin
      MannequinAuthor

      Examining the source of Ubuntu's python2.6 2.6.6-5ubuntu1 package
      suggests beyond the limits of the string is considered \W, like Perl.

      [Modules/_sre.c](https://github.com/python/cpython/blob/main/Modules/_sre.c):
         336  LOCAL(int)
         337  SRE_AT(SRE_STATE* state, SRE_CHAR* ptr, SRE_CODE at)
         338  {
         339      /* check if pointer is at given position */
         340
         341      Py_ssize_t thisp, thatp;
         ...
         365      case SRE_AT_BOUNDARY:
         366          if (state->beginning == state->end)
         367              return 0;
         368          thatp = ((void*) ptr > state->beginning) ?
         369              SRE_IS_WORD((int) ptr[-1]) : 0;
         370          thisp = ((void*) ptr < state->end) ?
         371              SRE_IS_WORD((int) ptr[0]) : 0;
         372          return thisp != thatp;
      

      SRE_IS_WORD() returns 16 for the 63 \w characters, 0 otherwise.

      This is born out by tests.

      Note, 366 above confirms it's never true for an empty string. The
      documentation states that \B "is just the opposite of \b" yet
      re.match(r'\b', '') returns None and so does \B so \B isn't the opposite
      of \b in all cases.

    4. poolie commented on Aug 22, 2011

      pooliemannequin
      Mannequin

      Note, 366 above confirms it's never true for an empty string. The
      documentation states that \B "is just the opposite of \b" yet
      re.match(r'\b', '') returns None and so does \B so \B isn't the opposite
      of \b in all cases.

      This is also a bit strange if you follow the Perl line of reasoning of imagining there are non-word characters outside the string. And, indeed, in Perl,

      "" =~ /\B/

      is true.

      So this patch adds some tests for \b behaviour and some docs. I think possible \B should actually change, but that would be a bigger (perhaps impossible?) change.

    5. ezio-melotti commented on Feb 27, 2012

      @ezio-melotti
      Member

      This is a new patch based on Martin work.
      I don't think it's necessary to explain what happens while using r'\b' or r'\B' on an empty string in the doc -- that's not a common case and it might end up confusing users. I think however that a couple of examples might help them figuring out what they are useful for.
      Mentioning that they work with the beginning/end of the string too is a reasonable request, so I tweaked the doc to point that out.

    6. merwok commented on Feb 27, 2012

      @merwok
      Member

      Like it.

    7. python-dev commented on Feb 29, 2012

      python-devmannequin
      Mannequin

      New changeset fc89e09ca2fc by Ezio Melotti in branch '2.7':
      bpo-10713: Improve documentation for \b and \B and add a few tests. Initial patch and tests by Martin Pool.
      http://hg.python.org/cpython/rev/fc89e09ca2fc

      New changeset cde7fa40b289 by Ezio Melotti in branch '3.2':
      bpo-10713: Improve documentation for \b and \B and add a few tests. Initial patch and tests by Martin Pool.
      http://hg.python.org/cpython/rev/cde7fa40b289

      New changeset b78ca038e468 by Ezio Melotti in branch 'default':
      bpo-10713: merge with 3.2.
      http://hg.python.org/cpython/rev/b78ca038e468

    8. ezio-melotti commented on Feb 29, 2012

      @ezio-melotti
      Member

      Fixed, thanks for the patch!

    9. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    docsDocumentation in the Doc direasytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions