我需要一个正则表达式能够匹配除以特定模式开头的字符串以外的所有内容(特别是index.php和后面的内容,如index.php?id=2342343)。
当前回答
正则表达式:匹配除了:
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\./。
其他回答
你可以在字符集的开头加上^来匹配除这些字符以外的任何字符。
[^=]*
将匹配除=
在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\./。
您可以从一开始就使用负向前,例如,^(?!foo)。*$不应该匹配以foo开头的任何内容。
只需匹配/^index\.php/,然后拒绝匹配它的任何东西。