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

basic regex rule fails to match after updating to 0.2 #314

Closed
mhristache opened this issue Jan 1, 2017 · 0 comments
Closed

basic regex rule fails to match after updating to 0.2 #314

mhristache opened this issue Jan 1, 2017 · 0 comments

Comments

@mhristache
Copy link

I have a very simple function used to strip ansi formatting, which used to work fine with regex 0.1:

extern crate regex;

fn main() {
    let input = "\x1b[34mfoo";
    assert_eq!(strip_formatting(input), "foo");
}

fn strip_formatting<'t>(input: &'t str) -> String {
    let re = regex::Regex::new(r#"\x1B\[.+?m"#).unwrap();
    re.replace_all(input, "")
}

After updating to regex 0.2, I did a small change to the function to adapt to the new API (no syntax change):

extern crate regex;
use std::borrow::Cow;

fn main() {
    let input = "\x1b[34mfoo";
    assert_eq!(strip_formatting(input), "foo");
}

fn strip_formatting<'t>(input: &'t str) -> Cow<'t, str> {
    let re = regex::Regex::new(r#"\x1B\[.+?m"#).unwrap();
    re.replace_all(input, "")
}

... but now it fails to match the regex rule:

   Compiling generic v0.0.1 (file:///Users/mX/projects/rust/tmp/generic)
    Finished debug [unoptimized + debuginfo] target(s) in 2.9 secs
     Running `target/debug/generic`
thread 'main' panicked at 'assertion failed: `(left == right)` (left: `"\u{1b}[34mfoo"`, right: `"foo"`)', src/main.rs:6
note: Run with `RUST_BACKTRACE=1` for a backtrace.

The strange thing is that it works if I replace with something instead of "", e.g. this works:

extern crate regex;
use std::borrow::Cow;

fn main() {
    let input = "\x1b[34mfoo";
    assert_eq!(strip_formatting(input), "something foo");
}

fn strip_formatting<'t>(input: &'t str) -> Cow<'t, str> {
    let re = regex::Regex::new(r#"\x1B\[.+?m"#).unwrap();
    re.replace_all(input, "something ")
}
@mhristache mhristache changed the title basic regex rule fails after updating to 0.2 basic regex rule fails to match after updating to 0.2 Jan 1, 2017
utkarshkukreti added a commit to utkarshkukreti/regex that referenced this issue Jan 2, 2017
bors added a commit that referenced this issue Jan 2, 2017
Fix replace_all when only match is at start and replacement is empty.

Fixes #314.

All old tests and the new test I added pass, and I also confirmed manually that the test case reported in #314 passes with this change. Let me know if you can think of any case that my change doesn't handle!
bors added a commit that referenced this issue Jan 2, 2017
Fix replace_all when only match is at start and replacement is empty.

Fixes #314.

All old tests and the new test I added pass, and I also confirmed manually that the test case reported in #314 passes with this change. Let me know if you can think of any case that my change doesn't handle!
bors added a commit that referenced this issue Jan 2, 2017
Fix replace_all when only match is at start and replacement is empty.

Fixes #314.

All old tests and the new test I added pass, and I also confirmed manually that the test case reported in #314 passes with this change. Let me know if you can think of any case that my change doesn't handle!
@bors bors closed this as completed in #315 Jan 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant