Skip to content

Feature request: sticky matching without relying on slicing, so that Match range corresponds to unsliced string #1390

Description

@jedwards1211

Describe your feature request

I want to be able to do find_at, captures_at etc, but without searching beyond the start index.

The reason I'm not happy with &Regex::new(r"^bar").find(&str[index..]) is the match range starts at 0, not at index. I want a Match whose range corresponds to the original string, not a slice.

I'm new to Rust, so maybe there's a smarter way to do this, but I'm trying to adapt a parsing approach I've used in other languages like

pub struct ParseState {
  str: String,
  idx: usize,
}

pub trait Parser {
  fn peek<'h>(&'h self, regex: &Regex) -> Option<Match<'h>>;
  fn find<'h>(&'h mut self, regex: &Regex) -> Option<Match<'h>>;
}

Where peek returns a Match if it exists at the current idx, and find does the same, but also advances the idx if found.

I've been able to use a sticky flag for this purpose in other languages. I wish I could just shift the Match range myself but Match::new is private. This is feeling pretty inconvenient. Seems like I'm going to have to go to the trouble to make some wrapper structs and implementations that apply the offset.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions