Skip to content

Strings that end with properly escaped backslashes cause error to be thrown in re.search/sub/etc. functions. #74319

Closed

Description

@PatrickFoley
BPO 30133
Nosy @ezio-melotti, @bitdancer, @serhiy-storchaka
Superseder
  • bpo-31714: Improve re documentation
  • Files
  • sample.py
  • 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 = None
    closed_at = <Date 2017-11-16.13:25:52.023>
    created_at = <Date 2017-04-21.21:18:05.459>
    labels = ['expert-regex', '3.7']
    title = 'Strings that end with properly escaped backslashes cause error to be thrown in re.search/sub/etc. functions.'
    updated_at = <Date 2017-11-16.13:25:52.022>
    user = 'https://bugs.python.org/PatrickFoley'

    bugs.python.org fields:

    activity = <Date 2017-11-16.13:25:52.022>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-11-16.13:25:52.023>
    closer = 'serhiy.storchaka'
    components = ['Regular Expressions']
    creation = <Date 2017-04-21.21:18:05.459>
    creator = 'Patrick Foley'
    dependencies = []
    files = ['46822']
    hgrepos = []
    issue_num = 30133
    keywords = []
    message_count = 7.0
    messages = ['292079', '292080', '292093', '292094', '292095', '292102', '306358']
    nosy_count = 5.0
    nosy_names = ['ezio.melotti', 'mrabarnett', 'r.david.murray', 'serhiy.storchaka', 'Patrick Foley']
    pr_nums = []
    priority = 'normal'
    resolution = 'out of date'
    stage = 'resolved'
    status = 'closed'
    superseder = '31714'
    type = None
    url = 'https://bugs.python.org/issue30133'
    versions = ['Python 2.7', 'Python 3.6', 'Python 3.7']

    Activity

    1. PatrickFoley commented on Apr 21, 2017

      PatrickFoleymannequin
      MannequinAuthor

      The following code demonstrates:

      import re
      text = 'ab\\'
      exp = re.compile('a')
      print(re.sub(exp, text, ''))

      If you remove the backslash(es), the code runs fine.

      This appears to be specific to the re module and only to strings that end in (even properly escaped) backslashes. You could easily receive raw data like this from freehand input sources so it would be nice not to have to remove trailing backslashes before running a regular expression.

    2. bitdancer commented on Apr 21, 2017

      @bitdancer
      Member

      I think you are missing a re.escape around text. Text is otherwise not a valid replacement pattern.

    3. mrabarnett commented on Apr 22, 2017

      mrabarnettmannequin
      Mannequin

      Yes, the second argument is a replacement template, not a literal.

      This issue does point out a different problem, though: re.escape will add backslashes that will then be treated as literals in the template, for example:

      >>> re.sub(r'a', re.escape('(A)'), 'a')
      '\\(A\\)'

      re.escape doesn't always help.

      The solution here is to pass a replacement function instead:

      >>> re.sub(r'a', lambda m: '(A)', 'a')
      '(A)'
    4. bitdancer commented on Apr 22, 2017

      @bitdancer
      Member

      Good point, re.escape is for literal text you want to insert into a matching pattern, but the replacement template isn't a matching pattern. Do we need a different escape function? I guess the function solution is enough?

    5. mrabarnett commented on Apr 22, 2017

      mrabarnettmannequin
      Mannequin

      The function solution does have a larger overhead than a literal.

      Could the template be made more accepting of backslashes without breaking anything? (There's also bpo-29995 "re.escape() escapes too much", which might help.)

    6. serhiy-storchaka commented on Apr 22, 2017

      @serhiy-storchaka
      Member

      re.escape() shouldn't be used for a replacement template. You need just double backslashes when escape a literal string for a replacement template: s.replace('\\', '\\\\'). This should be documented if still is not documented.

    7. serhiy-storchaka commented on Nov 16, 2017

      @serhiy-storchaka
      Member

      The proper way of escaping the replacement string has been documented by bpo-31714.

    8. 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

    No one assigned

      Labels

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions