can1357/oh-my-pi · error · TacError

invalid regular expression

Error message

invalid regular expression: {0}

What it means

This error from the tac builtin (crates/pi-builtins/src/tac.rs, InvalidRegex) means the separator expression supplied via tac's -r/--regex option failed to compile with the regex crate. tac reverses files using a user-provided separator pattern; an invalid pattern cannot be used. The regex::Error is embedded and describes the exact syntax problem.

Solutions

  1. Fix the regex syntax reported by the wrapped error (e.g. close the bracket, escape specials).
  2. Test the pattern in a regex tool (e.g. `regex` crate syntax) before passing it to tac.
  3. If you meant a literal separator string, drop -r/--regex so the argument is treated literally.
  4. Quote the pattern properly in the shell: use single quotes to avoid backslash mangling.

Example fix

// before
tac -r '[sep]'  // invalid regular expression: unclosed character class

// after
tac -r '\[sep\]'
Defensive patterns

Strategy: validation

Validate before calling

const { Regex } = require('regex');
function isValidRegex(pattern) {
  try { new Regex(pattern); return true; } catch { return false; }
}

Type guard

function isInvalidRegexError(err) {
  return err instanceof Error && err.message.startsWith('invalid regular expression: ');
}

Try / catch

try {
  await tac.run(['-r', pattern, file]);
} catch (err) {
  if (String(err).startsWith('invalid regular expression')) {
    console.error(`bad separator pattern '${pattern}', falling back to literal newline`);
  } else throw err;
}

Prevention

When it happens

Trigger: Running tac with `-r/--regex` and a pattern that fails regex compilation, e.g. `tac -r '[unclosed'` or `tac -r 'a{2,1}'` (invalid repetition bounds).

Common situations: Hand-written patterns with unescaped special characters (`(`, `[`, `*` in the wrong place); shell quoting stripping backslashes (`tac -r '\n'` semantics); patterns ported from other regex dialects with unsupported syntax.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/1b512f4cba7d38e0. Report an issue: GitHub.

Appendix: source

Thrown at crates/pi-builtins/src/tac.rs:30

use clap::{Arg, ArgAction, ArgMatches, Command};
use memchr::memmem;
use memmap2::Mmap;
use thiserror::Error;
use uucore::display::Quotable;

use crate::host::{Host, Utility, format_usage, matches_parser, util};

mod options {
	pub static BEFORE: &str = "before";
	pub static REGEX: &str = "regex";
	pub static SEPARATOR: &str = "separator";
	pub static FILE: &str = "file";
}

#[derive(Debug, Error)]
enum TacError {
	/// A regular expression given by the user is invalid.
	#[error("invalid regular expression: {0}")]
	InvalidRegex(regex::Error),
	/// An error opening a file for reading.
	#[error("failed to open {} for reading: {}", .0.quote(), strip_errno(.1))]
	Open(OsString, std::io::Error),
	/// An error reading the contents of a file or stdin.
	#[error("{}: read error: {}", .0.maybe_quote(), strip_errno(.1))]
	Read(OsString, std::io::Error),
	/// An error writing the reversed contents of a file or stdin.
	#[error("failed to write to stdout: {}", strip_errno(.0))]
	Write(std::io::Error),
}

fn strip_errno(error: &std::io::Error) -> String {
	let mut message = error.to_string();
	if let Some(position) = message.find(" (os error ") {
		message.truncate(position);
	}
	message

View on GitHub (pinned to 9690622007)