我需要一个正则表达式能够匹配除以特定模式开头的字符串以外的所有内容(特别是index.php和后面的内容,如index.php?id=2342343)。


只需匹配/^index\.php/,然后拒绝匹配它的任何东西。


您可以从一开始就使用负向前,例如,^(?!foo)。*$不应该匹配以foo开头的任何内容。


在Python中:

>>> import re
>>> p='^(?!index\.php\?[0-9]+).*$'
>>> s1='index.php?12345'
>>> re.match(p,s1)
>>> s2='index.html?12345'
>>> re.match(p,s2)
<_sre.SRE_Match object at 0xb7d65fa8>

你可以在字符集的开头加上^来匹配除这些字符以外的任何字符。

[^=]*

将匹配除=


正则表达式:匹配除了:

a string starting with a specific pattern (e.g. any - empty, too - string not starting with foo): Lookahead-based solution for NFAs: ^(?!foo).*$ ^(?!foo) Negated character class based solution for regex engines not supporting lookarounds: ^(([^f].{2}|.[^o].|.{2}[^o]).*|.{0,2})$ ^([^f].{2}|.[^o].|.{2}[^o])|^.{0,2}$ a string ending with a specific pattern (say, no world. at the end): Lookbehind-based solution: (?<!world\.)$ ^.*(?<!world\.)$ Lookahead solution: ^(?!.*world\.$).* ^(?!.*world\.$) POSIX workaround: ^(.*([^w].{5}|.[^o].{4}|.{2}[^r].{3}|.{3}[^l].{2}|.{4}[^d].|.{5}[^.])|.{0,5})$ ([^w].{5}|.[^o].{4}|.{2}[^r].{3}|.{3}[^l].{2}|.{4}[^d].|.{5}[^.]$|^.{0,5})$ a string containing specific text (say, not match a string having foo): Lookaround-based solution: ^(?!.*foo) ^(?!.*foo).*$ POSIX workaround: Use the online regex generator at www.formauri.es/personal/pgimeno/misc/non-match-regex a string containing specific character (say, avoid matching a string having a | symbol): ^[^|]*$ a string equal to some string (say, not equal to foo): Lookaround-based: ^(?!foo$) ^(?!foo$).*$ POSIX: ^(.{0,2}|.{4,}|[^f]..|.[^o].|..[^o])$ a sequence of characters: PCRE (match any text but cat): /cat(*SKIP)(*FAIL)|[^c]*(?:c(?!at)[^c]*)*/i or /cat(*SKIP)(*FAIL)|(?:(?!cat).)+/is Other engines allowing lookarounds: (cat)|[^c]*(?:c(?!at)[^c]*)* (or (?s)(cat)|(?:(?!cat).)*, or (cat)|[^c]+(?:c(?!at)[^c]*)*|(?:c(?!at)[^c]*)+[^c]*) and then check with language means: if Group 1 matched, it is not what we need, else, grab the match value if not empty a certain single character or a set of characters: Use a negated character class: [^a-z]+ (any char other than a lowercase ASCII letter) Matching any char(s) but |: [^|]+

演示注意:换行符\n在演示中被使用在负字符类中,以避免匹配溢出到相邻的行。在测试单个字符串时,它们是不必要的。

锚注:在许多语言中,使用\A定义字符串的明确开头,使用\z(在Python中是\z,在JavaScript中是$)定义字符串的结尾。

点注:在许多口味(但不包括POSIX, TRE, TCL),。匹配除换行字符以外的任何字符。确保你使用了相应的DOTALL修饰符(PCRE/Boost/ /s)。NET/Python/Java和Ruby中的/m)。匹配包含换行符的任何字符。

反斜杠注意:在语言中,你必须声明允许转义序列的C字符串模式(如\n表示换行符),你需要双反斜杠来转义特殊字符,以便引擎可以将它们视为字面字符(例如在Java中,world\。将被声明为"world\\.",或者使用字符类:"world[.]")。使用原始字符串字面量(Python r'\bworld\b'), c#逐字字符串字面量@"world\.",或斜杠字符串/正则表达式字面量符号,如/world\./。


经过长时间的搜索,我发现了这条线索。我在多次搜索和替换某些事件时遇到了这个问题。但我用的模式一直匹配到最后。在下面的例子

import re

text = "start![image]xxx(xx.png) yyy xx![image]xxx(xxx.png) end"
replaced_text = re.sub(r'!\[image\](.*)\(.*\.png\)', '*', text)
print(replaced_text)

gave

start* end

基本上,正则表达式从第一个![图像]匹配到最后一个。png,吞没了中间的yyy

使用Firish在https://stackoverflow.com/a/17761124/429476上面发布的方法来打破事件之间的匹配。这里的空间不匹配;因为单词是用空格隔开的。

replaced_text = re.sub(r'!\[image\]([^ ]*)\([^ ]*\.png\)', '*', text)

得到了我想要的

start* yyy xx* end