Summary
Sed patterns with backreferences (`\1`, `\2`, etc.) in the search/match side of a substitution fail with `backreferences are not supported`. Backreferences in the replacement side work correctly.
Reproduction
# Replacement backreference — WORKS:
echo "abc" | sed "s/a\(b\)c/\1/"
# Output: b
# Search-side backreference — FAILS:
echo "<a href='tag_hello.html'>hello</a>" | sed "s|<a href='tag_\([^']*\).html'>\1</a>|\1|g"
# Error: backreferences are not supported
The Rust `regex` crate does not support backreferences in patterns. This is a known limitation of the crate.
Context
Bashblog uses search-side backreferences in the `edit()` function to strip tag links back to plain text:
sed "/^<p>$template_tags_line_header/s|<a href='$prefix_tags\([^']*\).html'>\\1</a>|\\1|g"
This pattern captures the tag name, then uses `\1` in the search to ensure the tag text matches the href, then replaces the whole `` tag with just the tag name.
Possible approaches
- Use `regex-lite` or `fancy-regex` crate which supports backreferences
- Implement a custom backreference handler that does two-pass matching
- Gracefully degrade: emit a warning and skip the substitution instead of crashing
Expected behavior
`sed` should support backreferences (`\1` through `\9`) in both the search and replacement portions of `s` commands, matching GNU sed behavior.
Summary
Sed patterns with backreferences (`\1`, `\2`, etc.) in the search/match side of a substitution fail with `backreferences are not supported`. Backreferences in the replacement side work correctly.
Reproduction
The Rust `regex` crate does not support backreferences in patterns. This is a known limitation of the crate.
Context
Bashblog uses search-side backreferences in the `edit()` function to strip tag links back to plain text:
sed "/^<p>$template_tags_line_header/s|<a href='$prefix_tags\([^']*\).html'>\\1</a>|\\1|g"This pattern captures the tag name, then uses `\1` in the search to ensure the tag text matches the href, then replaces the whole `` tag with just the tag name.
Possible approaches
Expected behavior
`sed` should support backreferences (`\1` through `\9`) in both the search and replacement portions of `s` commands, matching GNU sed behavior.