Regular expression matching everything except a given regular expression

后端 未结 4 1885
[愿得一人]
[愿得一人] 2020-12-31 07:39

I am trying to figure out a regular expression which matches any string that doesn\'t start with mpeg. A generalization of this is matching any string which doesn\'t start w

相关标签:
4条回答
  • 2020-12-31 08:17

    Try a look-ahead assertion:

    (?!mpeg)^.*
    

    Or if you want to use negated classes only:

    ^(.{0,3}$|[^m]|m([^p]|p([^e]|e([^g])))).*$
    
    0 讨论(0)
  • 2020-12-31 08:28
    ^(?!mpeg).*
    

    This uses a negative lookahead to only match a string where the beginning doesn't match mpeg. Essentially, it requires that "the position at the beginning of the string cannot be a position where if we started matching the regex mpeg, we could successfully match" - thus matching anything which doesn't start with mpeg, and not matching anything that does.

    However, I'd be curious about the context in which you're using this - there might be other options aside from regex which would be either more efficient or more readable, such as...

    if not inputstring.startswith("mpeg"):
    
    0 讨论(0)
  • 2020-12-31 08:33

    don't lose your mind with regex.

    if len(mystring) >=4 and mystring[:4]=="mpeg":
        print "do something"
    

    or use startswith() with "not" keyword

    if len(mystring)>=4 and not mystring.startswith("mpeg")
    
    0 讨论(0)
  • 2020-12-31 08:38

    Your regexp wouldn't match "npeg", I think you would need come up with ^($|[^m]|m($|[^p]|p($|[^e]|e($|[^g])))), which is quite horrible. Another alternative would be ^(.{0,3}$|[^m]|.[^p]|..[^e]|...[^g]) which is only slightly better.

    So I think you should really use a look-ahead assertion as suggested by Dav and Gumbo :-)

    0 讨论(0)
提交回复
热议问题