0

The number must not start with 0.

I ran this grep command with reg ex

❯ echo "#time 1m" | grep -E -o "#time\s(?!0)\d{1,2}[m|h|d]"

and get the following output:

zsh: event not found: 0

My attempt in a graphical reg ext tester shows me a match:enter image description here

svebal
  • 3

2 Answers2

4
zsh: event not found: 0

This is due to the shell treating the ! as the trigger for history expansion. Either put the string in single quotes, or disable history expansion with set +o histexpand (or setopt nohistexpand in zsh, or set +H in Bash). See e.g. Understanding the exclamation mark (!) in bash

... grep -E -o "#time\s(?!0)\d{1,2}[m|h|d]"

Note that \s, (?!...), and \d are part of Perl regexes, not standard extended regexes that grep -E uses. Also, [m|h|d] matches any single character that is one of m, |, h, or d. It'd be better written as [mhd|] or so, but you probably mean [mhd] (or (m|h|d) which is just a longer way of writing it).

You'll likely have to rewrite the regex as a standard ERE, or switch to a tool that can use Perl regexes, like grep -P in GNU grep.

See Why does my regular expression work in X but not in Y?

ilkkachu
  • 138,973
0

This is nothing to do with the RE. It's the shell being allowed to see inside your string and trying to evaluate it. (That's why the error message starts with zsh rather than, say, grep.) To prevent this, use single quotes (') instead of double quotes (")

Chris Davies
  • 116,213
  • 16
  • 160
  • 287