如何告诉egrep不接受哪些字符

发布于 2024-10-26 17:02:32 字数 264 浏览 1 评论 0原文

我想接受以 s 开头的字符串,而下一个字符(无论它是什么)必须在脚本中再包含两次(但不能少,不能多),并且在此字符不能是反斜杠之前。所以:

s(.)[now some chars except (.) and not ending with \]\1[some chars but not (.) and not ending with \]\1[some chars but not (.)]

\1 和 (.) 和 s 是正则表达式的真实部分

I want to accept string that begins with s than the next character (whatever it is) must be conatined in script two more times (but not less, not more) and before this char cannot be a backslash. So:

s(.)[now some chars except (.) and not ending with \]\1[some chars but not (.) and not ending with \]\1[some chars but not (.)]

\1 and (.) and s are real part of regex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

知你几分 2024-11-02 17:02:32

我不认为 egrep 会削减它。
您需要一个可以执行前瞻断言的 grep,原因如下:

/^ s (.) (?:(?:\\.|(?!\1).)+\1){2} (?: \\.|(?!\1).)+ $/xs

/^             # Begin of string
     s
     (.)                # capture first char after 's'
     (?:                # Begin Grp
         (?: \\.          # begin grp, escaped anything
           | (?!\1).        # OR, any char that is not (.)
         )+               # end grp, do this more than once
         \1               # Followed by (.)
     ){2}               # End Grp, do this exactly twice

     (?: \\.            # begin grp, escaped anything
       | (?!\1).           # OR, anchar that is not (.)
     )+                 # end grp, do this more than once

 $/xs         # End of string, x and s modifiers

I don't think egrep is going to cut it.
You need a grep that can do lookahead assertions, here's why:

/^ s (.) (?:(?:\\.|(?!\1).)+\1){2} (?:\\.|(?!\1).)+ $/xs

/^             # Begin of string
     s
     (.)                # capture first char after 's'
     (?:                # Begin Grp
         (?: \\.          # begin grp, escaped anything
           | (?!\1).        # OR, any char that is not (.)
         )+               # end grp, do this more than once
         \1               # Followed by (.)
     ){2}               # End Grp, do this exactly twice

     (?: \\.            # begin grp, escaped anything
       | (?!\1).           # OR, anchar that is not (.)
     )+                 # end grp, do this more than once

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