我需要一个能够匹配所有内容的正则表达式but以特定模式开头的字符串(具体而言index.php
以及接下来的内容,例如index.php?id=2342343
).
正则表达式:匹配所有内容but:
- a string starting with a specific pattern (e.g. any - empty, too - string not starting with
foo
):
- Lookahead-based solution for NFAs:
- 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:
- POSIX workaround:
- 使用在线正则表达式生成器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:
- POSIX:
- ^(.{0,2}|.{4,}|[^f]..|.[^o].|..[^o])$
- a sequence of characters:
-
PCRE(匹配任何文本,但
cat
): /cat(*SKIP)(*FAIL)|[^c]*(?:c(?!at)[^c]*)*/i or /cat(*SKIP)(*FAIL)|(?:(?!cat).)+/is
- 其他允许环视的引擎:(cat)|[^c]*(?:c(?!at)[^c]*)* (or (?s)(cat)|(?:(?!cat).)*, or
(cat)|[^c]+(?:c(?!at)[^c]*)*|(?:c(?!at)[^c]*)+[^c]*
),然后检查语言含义:如果第 1 组匹配,则不是我们需要的,否则,如果不为空,则获取匹配值
- a certain single character or a set of characters:
- Use a 否定字符类: [^a-z]+(除小写 ASCII 字母之外的任何字符)
- 匹配任何字符,但是
|
: [^|]+
演示笔记: 换行符\n
在演示中的否定字符类中使用,以避免匹配溢出到相邻行。测试单个字符串时它们不是必需的。
锚注:在许多语言中,使用\A
定义字符串的明确开头,以及\z
(在Python中,它是\Z
,在 JavaScript 中,$
是可以的)来定义字符串的末尾。
Dot note:有多种风格(但不包括 POSIX、TRE、TCL),.
匹配任何字符但换行符字符。确保使用相应的 DOTALL 修饰符(/s
在 PCRE/Boost/.NET/Python/Java 和/m
在 Ruby 中).
匹配包括换行符在内的任何字符。
反斜杠注释:在必须使用允许转义序列的 C 字符串声明模式的语言中(例如\n
对于换行符),您需要将转义特殊字符的反斜杠加倍,以便引擎可以将它们视为文字字符(例如在 Java 中,world\.
将被声明为"world\\."
,或使用字符类:"world[.]"
)。使用原始字符串文字 (Pythonr'\bworld\b'
), C# 逐字字符串文字@"world\."
,或斜杠字符串/正则表达式文字符号,例如/world\./
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)