unix 中正则表达式的语法错误

发布于 2024-11-26 13:19:28 字数 305 浏览 6 评论 0原文

我尝试找到一个与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

披肩女神 2024-12-03 13:19:28

这应该匹配 1 到 999 之间的任何数字。我添加了 {0,2} ,这意味着匹配 [0-9] 字符组的 0 到 2 次重复。

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

如果您尝试匹配括号和管道字符,那么您需要转义它们:

 ^\([1-9][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.

 ^[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}\|\)$
知你几分 2024-12-03 13:19:28

将正则表达式用双引号引起来,或使用反斜杠转义括号和管道。

例如 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]?|)$" or egrep ^\([1-9][0-9]?\|\)$.

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

红墙和绿瓦 2024-12-03 13:19:28

试试这个:

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

Try this one:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文