开发者

How to tell egrep which characters not to accept [duplicate]

This question already has an answer here: Learning Regular Expressions [closed] (1 answer) Closed 6 years ago.

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 \]\开发者_Python百科1[some chars but not (.)]

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜