Skip to content

add example using curly braces in replacement string #333

Closed
@lilith

Description

@lilith

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_" );

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions