Skip to content

grep - -E rejects a repeat operator right after ^ or $ (^*, $*, ^+x) #206

Open

Description

@sylvestre

In an extended regexp GNU grep accepts a repetition operator that follows an anchor. After
^ it warns * at start of expression (or +, ?) and searches; after $ it accepts it
silently. uu_grep exits 2 with Oniguruma's target of repeat operator is invalid.

Related: #164 covers an operator after ( or |, and #48 strips one at the very start of
the pattern; neither handles an operator after an anchor (Oniguruma reports a different error
code there).

Exposed by the GNU tests spencer1 (tests #53 ^* and #54 $*) and spencer1-locale
(the same two tests in each locale, #209-#216).

Reproduction

Rust (incorrect)

$ printf 'q\n' | ./target/release/grep -E '^*q'
grep: invalid pattern "^*q": target of repeat operator is invalid
# Exit code: 2

$ printf 'q\n' | ./target/release/grep -E 'q$*'
grep: invalid pattern "q$*": target of repeat operator is invalid
# Exit code: 2

$ printf 'q\n' | ./target/release/grep -E '(^+q)'
grep: invalid pattern "(^+q)": target of repeat operator is invalid
# Exit code: 2

GNU (correct)

$ printf 'q\n' | LC_ALL=C /usr/bin/grep -E '^*q'
/usr/bin/grep: warning: * at start of expression
q
# Exit code: 0

$ printf 'q\n' | LC_ALL=C /usr/bin/grep -E 'q$*'
q
# Exit code: 0

$ printf 'q\n' | LC_ALL=C /usr/bin/grep -E '(^+q)'
/usr/bin/grep: warning: + at start of expression
q
# Exit code: 0

Test

#[test]
fn ere_repeat_after_anchor() {
    let (_s, mut c) = ucmd();
    c.args(&["-E", "^*jo"])
        .pipe_in("jo\nx\n")
        .succeeds()
        .stdout_is("jo\n")
        .stderr_contains("* at start of expression");

    let (_s, mut c) = ucmd();
    c.args(&["-E", "jo$+"])
        .pipe_in("jo\nx\n")
        .succeeds()
        .stdout_only("jo\n");
}

Exposed by the GNU grep test suite: spencer1, spencer1-locale.

Activity

  1. added
    bug
    good first issue
    gnu-compat
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggnu-compatgood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions