denisidoro/navi · error

Invalid regex

Error message

Invalid regex

What it means

This panic comes from `.expect("Invalid regex")` when compiling the delimiter as a regex in `get_column` (src/finder/post.rs:46). The user-supplied `--delimiter` option is passed straight to `regex::Regex::new`, so any syntactically invalid regex pattern causes a hard panic instead of a graceful error. The default delimiter `\s\s+` is valid, so this only fires when a custom delimiter string is provided.

Source

Thrown at src/finder/post.rs:46

            )
        };

        let output = shell::out()
            .arg(cmd.as_str())
            .stderr(Stdio::inherit())
            .output()
            .context("Failed to execute map function")?;

        String::from_utf8(output.stdout).context("Invalid utf8 output for map function")
    } else {
        Ok(text)
    }
}

fn get_column(text: String, column: Option<u8>, delimiter: Option<&str>) -> String {
    if let Some(c) = column {
        let mut result = String::from("");
        let re = regex::Regex::new(delimiter.unwrap_or(r"\s\s+")).expect("Invalid regex");
        for line in text.split('\n') {
            if (line).is_empty() {
                continue;
            }
            let mut parts = re.split(line).skip((c - 1) as usize);
            if !result.is_empty() {
                result.push('\n');
            }
            result.push_str(parts.next().unwrap_or(""));
        }
        result
    } else {
        text
    }
}

pub fn process(
    text: String,

View on GitHub (pinned to f7330b9ad5)

Solutions

  1. Fix the delimiter pattern so it is a valid Rust `regex` crate expression (no backreferences, no lookarounds, balanced groups/brackets, no trailing escape)
  2. If you meant a literal separator, escape metacharacters or use a pattern matching it, e.g. delimiter `|` → `\|`, tab → `\t` (actual tab character also works)
  3. Patch the code to propagate instead of panicking: match on `Regex::new(...)` and return an `Err` with the regex error so the user sees which pattern failed
  4. Validate the pattern beforehand with `regex::Regex::new(d).is_ok()` when accepting delimiter input in scripts or wrappers

Example fix

// before
let re = regex::Regex::new(delimiter.unwrap_or(r"\s\s+")).expect("Invalid regex");
// after
let re = regex::Regex::new(delimiter.unwrap_or(r"\s\s+"))
    .map_err(|e| anyhow!("Invalid regex '{}': {}", delimiter.unwrap_or(""), e))?;
Defensive patterns

Strategy: validation

Validate before calling

// Validate the delimiter before passing it to the finder:
fn delimiter_is_valid(delimiter: &str) -> bool {
    regex::Regex::new(delimiter).is_ok()
}
// e.g.
// assert!(delimiter_is_valid("\|"), "--delimiter must be a valid regex");

Type guard

fn as_valid_regex(delimiter: Option<&str>) -> Option<String> {
    delimiter.filter(|d| regex::Regex::new(d).is_ok()).map(String::from)
}

Try / catch

// Wrap calls that reach get_column when you cannot pre-validate; in Rust
// panics are caught with catch_unwind, but the better pattern is fixing the input:
let result = std::panic::catch_unwind(|| navi_process(text, column, Some(bad_delimiter), None));
match result {
    Ok(out) => out,
    Err(_) => eprintln!("delimiter '{}' is not a valid regex", bad_delimiter),
}

Prevention

When it happens

Trigger: Calling the finder's `process` (e.g. via the `--delimiter` CLI flag or the config's delimiter field) with a `column` set and a delimiter string that is not a valid regex — e.g. a lone `\` (backslash), an unclosed group like `(` or `[`, an invalid repetition like `*foo`, or a stray closing `)`.

Common situations: Users pass a literal separator such as `\t` or `|` intending a plain string, but escape it incorrectly (`\t` becomes a bare backslash + t is fine, but trailing `\` or half-written patterns panic); copying regex fragments from other tools with syntax the Rust `regex` crate rejects (backreferences like `\1`, lookarounds `(?=...)`); shell quoting stripping or mangling backslashes before the pattern reaches the code.

Related errors


AI-assisted analysis of denisidoro/navi@f7330b9ad5 (2026-09-03). Data as JSON: /api/errors/b2126035a4da22e9. Report an issue: GitHub.