Skip to content

fix(sed): backreferences in search pattern fail (\1 in match side) #1159

@chaliy

Description

@chaliy

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

  1. Use `regex-lite` or `fancy-regex` crate which supports backreferences
  2. Implement a custom backreference handler that does two-pass matching
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions