syntax error with a regex in unix

2020-04-19 04:58发布

问题:

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

回答1:

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}\|\)$


回答2:

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]?$"?



回答3:

Try this one:

echo $number | grep '^[1-9][0-9]\{0,2\}$'