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

时间:2011-03-27 13:38:34

标签: regex grep

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

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是正则表达式的真正部分

1 个答案:

答案 0 :(得分:0)

我认为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