8 releases

0.2.5 Nov 11, 2021
0.2.4 May 31, 2019
0.2.3 Dec 28, 2018
0.2.1 Aug 30, 2018
0.1.1 Aug 29, 2018

#578 in Text processing

Download history 583/week @ 2023-12-06 741/week @ 2023-12-13 492/week @ 2023-12-20 471/week @ 2023-12-27 577/week @ 2024-01-03 750/week @ 2024-01-10 882/week @ 2024-01-17 943/week @ 2024-01-24 1167/week @ 2024-01-31 1104/week @ 2024-02-07 1580/week @ 2024-02-14 1327/week @ 2024-02-21 1155/week @ 2024-02-28 1080/week @ 2024-03-06 1155/week @ 2024-03-13 806/week @ 2024-03-20

4,473 downloads per month
Used in 9 crates (7 directly)

MIT/Apache

32KB
632 lines

sedregex

pipeline status crates.io docs.rs

[Release docs]

[Master docs]

A simple sed-like library that uses regex under the hood.

Usage

There are basically two public interfaces which you can enjoy:

  • find_and_replace, which is useful for one-time processing of a bunch of commands,
  • and ReplaceCommand when you need to run the same command multiple times.

Examples

Let's jump straight to the examples!

use sedregex::{find_and_replace, ReplaceCommand};
fn main() {
    // Both case-insensitive and global:
    assert_eq!(
        "Please stop wuts oh wut.",
        ReplaceCommand::new("s/lol/wut/ig").unwrap().execute("Please stop Lols oh lol."),
    );

    // Multiple commands in one go:
    assert_eq!(
        "Please stop nos oh no.",
        find_and_replace("Please stop Lols oh lol.", &["s/lol/wut/ig", "s/wut/no/g"]).unwrap()
    );

    // Same, but skipping the `s` character.
    assert_eq!(
        "Please stop wuts oh wut.",
        find_and_replace("Please stop Lols oh lol.", &["/lol/wut/ig"]).unwrap()
    );

    // Skipping the flags.
    assert_eq!(
        "Please stop Lols oh wut.",
        find_and_replace("Please stop Lols oh lol.", &["/lol/wut/"]).unwrap()
    );

    // Skipping the last slash.
    assert_eq!(
        "Please stop Lols oh wut.",
        find_and_replace("Please stop Lols oh lol.", &["/lol/wut"]).unwrap()
    );

    // Escaping a slash in a replace part.
    assert_eq!(
        r"Please stop wut/s oh wut/.",
        find_and_replace("Please stop Lols oh lol.", &[r"s/lol/wut\//gi"]).unwrap()
    );

    // Some regex stuff.
    // Also note the lack of the trailing slash: it's opitonal!
    assert_eq!(
        "Second, First",
        find_and_replace(
            "First, Second",
            &[r"s/(?P<first>[^,\s]+),\s+(?P<last>\S+)/$last, $first"],
        ).unwrap()
    );

    // Ok let's go with some simple regex stuff.
    assert_eq!(
        "Some weird typo",
        find_and_replace("Some wierd typo", &[r"s/ie/ei/"]).unwrap()
    );
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~2.2–3MB
~54K SLoC