-
Notifications
You must be signed in to change notification settings - Fork 450
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
add example using curly braces in replacement string #333
Comments
This was an intended but undocumented breaking change. Note the docs for
and also
So this means that the reason why you're getting the behavior you see is that |
Ah. I was hoping that replacements would only expand if existent in the original regex. It'd probably be good to emphasize this, and stick to the curly brace syntax for examples. I'm not aware of other regex implementations with this behavior, thus my confusion :) I tend to expect that identifiers don't start with numbers. |
@nathanaeljones Go's regexp engine has precisely the same semantics. But yes, better docs here would be great. |
This shows how to use curly braces in the replacement string, and more specifically, explains why they are sometimes necessary. Fixes rust-lang#333
This used to work:
assert_eq!(Regex::new("(.)").unwrap().replace_all("a", "$1_"), "a_" );
But now produces "" instead of "a_".
It would appear that underscores somehow cause the replacement to be deleted instead of added.
Named captures don't fix it:
assert_eq!(Regex::new("(?P<char>.)").unwrap().replace_all("a", "$char_"), "a_" ); //actual: ""
But inserting a space between the capture and underscore works!
assert_eq!(Regex::new("(.)").unwrap().replace_all("a", "$1 _"), "a _" );
assert_eq!(Regex::new("(?P<char>.)").unwrap().replace_all("a", "$char _"), "a _" );
As does using curly braces around the identifier (the only apparent workaround)
assert_eq!(Regex::new("(?P<char>.)").unwrap().replace_all("a", "${char}_"), "a_" );
The text was updated successfully, but these errors were encountered: