Giter Club home page Giter Club logo

Comments (5)

iwillspeak avatar iwillspeak commented on May 27, 2024

You are right, onig implements replacement. The second argument to the replace family of fuctions is a Replacer. This can either be a literal string, a callback which takes the capture group and returns a astring, or some custom instance of your own type.

If you want to use the captures in the replacement the quickest way would probably be to format the captures yourself:

let result = re.replace_all("first second", |caps: &Captures| {
    format("{} {}", caps.at(2).unwrap(), caps.at(1).unwrap())
});

Check out the docs for more information.

from rust-onig.

DoumanAsh avatar DoumanAsh commented on May 27, 2024

But onig doesn't support $1 and etc when providing only replacement string, right?

from rust-onig.

iwillspeak avatar iwillspeak commented on May 27, 2024

No. The replacements that onig supports out of the box are:

  • Literal strings, with no support for capture group references
  • Callbacks to produce a replacement from the captured content

You could add support for dollar-string syntax quite easily though:

use onig::{Regex, Replacer, Captures};
use std::borrow::Cow;

/// A string, with `$1` refering to the first capture group.
struct Dollarified<'a>(&'a str);

impl <'a> Replacer for Dollarified<'a> {
    fn reg_replace(&mut self, caps: &Captures) -> Cow<str> {
        let mut replacement = self.0.to_owned();
        for i in 1..caps.len() {
            replacement = replacement.replace(
                &format!("${}", i),
                caps.at(i).unwrap());
        }
        replacement.into()
    }
}

fn main() {
    let re = Regex::new(r"(\w+) (\w+)").unwrap();
    let hay = "hello world";
    println!("{} -> {}", &hay, re.replace(hay, Dollarified("$2 $1")));
}

I can't vouch for the efficiency of the above code. If you know the format of the replacement then using a closure instead is going to be quicker.

from rust-onig.

DoumanAsh avatar DoumanAsh commented on May 27, 2024

Ok, I see.
Then I suppose user should go with callback to handle it.
Thanks.

from rust-onig.

iwillspeak avatar iwillspeak commented on May 27, 2024

I've added an example which shows a better implementation of dollar replacement strings. It is probably something we should support in this crate. More work is needed though to decide on the exact replacement syntax (do we support named captures, what does an invalid number expand to and so on).

Thanks for the question though!

from rust-onig.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.