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

replace does not work against regex '^'. #393

Closed
fulara opened this issue Aug 20, 2017 · 0 comments
Closed

replace does not work against regex '^'. #393

fulara opened this issue Aug 20, 2017 · 0 comments
Labels

Comments

@fulara
Copy link
Contributor

fulara commented Aug 20, 2017

I was just trying different corner cases with rust regex and I came across this one:
Code:

extern crate regex;
use regex::Regex;
fn main() {
    let regex = Regex::new("^").unwrap();
    let s = regex.replace("_", "begin").into_owned();
    assert_eq!("begin_", s); // will fail, string still '_'
}

Above fails. and below succeeds:

extern crate regex;
use regex::Regex;
fn main() {
    let regex = Regex::new("$").unwrap();
    let s = regex.replace("_", "begin").into_owned();
    assert_eq!("_begin", s); // will pass.
}

Tested against 0.2.2.

I tried testing the same case with boost regex and sed and both of these yielded expected result.

@BurntSushi BurntSushi added the bug label Aug 20, 2017
Lapin0t added a commit to Lapin0t/regex that referenced this issue Aug 20, 2017
Lapin0t added a commit to Lapin0t/regex that referenced this issue Oct 15, 2017
bors added a commit that referenced this issue Nov 9, 2017
Fix `Regex::replacen` when replacing an empty match.

Fixes #393, #394.

There was some logic error when deciding to return `Cow::Borrowed` vs `Cow::Owned` in the fast-path. I took the same solution as the slow-path, ie use `peekable` on the iterator. I'm not sure this is the most efficient way (maybe just add `mut did_replace = false` and set it to true in the loop), but it does the job.
@bors bors closed this as completed in #395 Nov 9, 2017
ethanpailes pushed a commit to ethanpailes/regex that referenced this issue Nov 29, 2017
ethanpailes pushed a commit to ethanpailes/regex that referenced this issue Dec 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants