syntax error with a regex in unix

2020-04-19 05:14发布

I tried find a regular expression that matches any number between 1 and 999. When is uses hooks I get a syntax error

(bash: syntax error near unexpected token `(')

and when I don't use the hooks nothing happens.

my regex is:

egrep ^([1-9][0-9]?|)$ Numbers

update:

but how can i get him to check the file i want him to check, because know i can check the numbers if i use echo but i should check the file

3条回答
够拽才男人
2楼-- · 2020-04-19 05:38

This should match any number between 1 and 999. I added the {0,2} which means match between 0 and 2 repeats of the [0-9] character group.

 ^[1-9][0-9]{0,2}$

If you are trying to match the parenthesis and pipe character, then you'll need to escape them:

 ^\([1-9][0-9]{0,2}\|\)$
查看更多
聊天终结者
3楼-- · 2020-04-19 05:40

Enclose the regex in double-quotes or escape the parantheses and pipes with backslash.

e.g. egrep "^([1-9][0-9]?|)$" or egrep ^\([1-9][0-9]?\|\)$.

BTW, your regex seems to be broken. How about egrep "^[1-9][0-9]?[0-9]?$"?

查看更多
对你真心纯属浪费
4楼-- · 2020-04-19 05:46

Try this one:

echo $number | grep '^[1-9][0-9]\{0,2\}$'
查看更多
登录 后发表回答