Skip to content
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

have a way to search backwards for re #36095

Closed
phr mannequin opened this issue Feb 13, 2002 · 7 comments
Closed

have a way to search backwards for re #36095

phr mannequin opened this issue Feb 13, 2002 · 7 comments
Labels
topic-regex type-feature A feature request or enhancement

Comments

@phr
Copy link
Mannequin

phr mannequin commented Feb 13, 2002

BPO 516762
Nosy @loewis, @amauryfa
Superseder
  • bpo-3825: Major reworking of Python 2.5.2 re module
  • 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 2008-09-22.01:00:03.338>
    created_at = <Date 2002-02-13.03:22:55.000>
    labels = ['type-feature']
    title = 'have a way to search backwards for re'
    updated_at = <Date 2014-08-25.10:38:33.476>
    user = 'https://bugs.python.org/phr'

    bugs.python.org fields:

    activity = <Date 2014-08-25.10:38:33.476>
    actor = 'Drekin'
    assignee = 'niemeyer'
    closed = True
    closed_date = <Date 2008-09-22.01:00:03.338>
    closer = 'benjamin.peterson'
    components = ['None']
    creation = <Date 2002-02-13.03:22:55.000>
    creator = 'phr'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 516762
    keywords = []
    message_count = 7.0
    messages = ['53476', '53477', '53478', '72784', '72832', '73549', '225871']
    nosy_count = 7.0
    nosy_names = ['loewis', 'niemeyer', 'phr', 'amaury.forgeotdarc', 'timehorse', 'mrabarnett', 'Drekin']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = None
    status = 'closed'
    superseder = '3825'
    type = 'enhancement'
    url = 'https://bugs.python.org/issue516762'
    versions = []

    @phr
    Copy link
    Mannequin Author

    phr mannequin commented Feb 13, 2002

    There doesn't seem to be any reasonable way to search
    a string backwards for a regular expression, starting
    from a given character position. I notice that the
    underlying C regular expression implemention supports
    a direction flag.

    I propose adding a direction flag to the search function
    on match objects:

    r = re.compile(...)
    m = re.search(str, startpos=5000, endpos=-1, dir=-1)

    would search in str for r, starting at
    location 5000 and searching backwards through
    location 0 (the beginning of the string).

    This is useful in (for example) text editors where
    you want to be able to search forwards or backwards,
    or if you're parsing an html file and see a </table>
    and want to find the matching <table>, etc.

    phr

    @phr phr mannequin assigned niemeyer Feb 13, 2002
    @phr phr mannequin added the type-feature A feature request or enhancement label Feb 13, 2002
    @loewis
    Copy link
    Mannequin

    loewis mannequin commented Mar 27, 2002

    Logged In: YES
    user_id=21627

    Moved to feature requests tracker.

    @phr
    Copy link
    Mannequin Author

    phr mannequin commented Sep 29, 2003

    Logged In: YES
    user_id=72053

    Another way to do this would be add a direction flag to the
    regexp itself, like re.R (for reverse) if R isn't already in use:

       group  = re.search(pat, str, start, end, re.R)

    or something like that.

    I continue to want this feature. It's very handy for writing
    web scrapers, which I do fairly frequently.

    @mrabarnett
    Copy link
    Mannequin

    mrabarnett mannequin commented Sep 8, 2008

    Does this request still stand? I'm working on the re module at the moment.

    @amauryfa
    Copy link
    Member

    amauryfa commented Sep 9, 2008

    Why not? I needed such a feature some time ago.
    But if possible, it should be a keyword argument:
    re.search(..., backwards=True)
    similar to list.sort(reverse=True)

    @mrabarnett
    Copy link
    Mannequin

    mrabarnett mannequin commented Sep 22, 2008

    Implemented as part of bpo-3825.

    @Drekin
    Copy link
    Mannequin

    Drekin mannequin commented Aug 25, 2014

    Hello, I wanted to use this feature and found this issue. It is marked as closed, being a duplicate of another closed issue. However the feature is actually not present. In 2008 it was planned to be added by Matthew Barnett in bpo-3825, but the work of improving re module moved to bigger project under bpo-2636 which resulted in regex package to be evenually included in stdlib. So unless regex is included in stdlib, one should try regex package (e.g. pip install regex).

    I've put here this summary just in case there is someone like me who looked for the feature and found this issue.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    topic-regex type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants