unix 中正则表达式的语法错误
我尝试找到一个与 1 到 999 之间的任何数字匹配的正则表达式。 当使用钩子时,我会收到语法错误
(bash: syntax error near unexpected token `(')
,而当我不使用钩子时,什么也不会发生。
我的正则表达式是:
egrep ^([1-9][0-9]?|)$ Numbers
更新:
但是我怎样才能让他检查我想让他检查的文件,因为知道如果我使用 echo 我可以检查数字,但我应该检查文件
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该匹配 1 到 999 之间的任何数字。我添加了
{0,2}
,这意味着匹配 [0-9] 字符组的 0 到 2 次重复。如果您尝试匹配括号和管道字符,那么您需要转义它们:
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.If you are trying to match the parenthesis and pipe character, then you'll need to escape them:
将正则表达式用双引号引起来,或使用反斜杠转义括号和管道。
例如
egrep "^([1-9][0-9]?|)$"
或egrep ^\([1-9][0-9]?\|\) $。
顺便说一句,你的正则表达式似乎被破坏了。
egrep "^[1-9][0-9]?[0-9]?$"
怎么样?Enclose the regex in double-quotes or escape the parantheses and pipes with backslash.
e.g.
egrep "^([1-9][0-9]?|)$"
oregrep ^\([1-9][0-9]?\|\)$
.BTW, your regex seems to be broken. How about
egrep "^[1-9][0-9]?[0-9]?$"
?试试这个:
Try this one: