像这样?在# ^(?s)((?!X).)*W((?!Y).)*$
^
(?s)
(
(?! X )
.
)*
W
(
(?! Y )
.
)*
$
或者,用词界
^{pr2}$
编辑-不清楚您的意思是X<;->;W<;->;Y由空格分隔
或任何数量的字符。这个扩展的、带注释的示例说明了这两种方法。
祝你好运!
注意,(?add-remove)构造是一个修饰组。通常这是一种
在regex中嵌入诸如s(Dot All)、i(忽略大小写)等选项。
其中(?s)表示添加点All修饰符,(?si)是相同的,但也有忽略大小写。在# ^(?s)(?!.*(?:\bX\b\s+\bW\b|\bW\b\s+\bY\b))(?:.*\b(W)\b.*|.*)$
# This regex validates W is not preceded by X
# nor followed by Y.
# It also optionally finds W.
# Only fails if its invalid.
# If passed, can check if W present by
# examining capture group 1.
^ # Beginning of string
(?s) # Modifier group, with s = DOT_ALL
(?! # Negative looahead assertion
.* # 0 or more any character (dot-all is set, so we match newlines too)
(?:
\b X \b \s+ \b W \b # Trying to match X, 1 or more whitespaces, then W
| \b W \b \s+ \b Y \b # Or, Trying to match W, 1 or more whitespaces, then Y
# Substitute this to find any interval between XWY
# \b X \b .* \b W \b
# | \b W \b .* \b Y \b
)
)
# Still at start of line.
# If here, we didn't find any XW, nor WY.
# Opotioinally finds W in group 1.
(?:
.* \b
( W ) # (1), W
\b .*
|
.*
)
$ # End of string
本文探讨了复杂的正则表达式应用案例,包括如何精确匹配特定字符串模式而不被周围字符干扰的方法。通过详细解析正则表达式的构造及使用场景,帮助读者深入理解正则表达式的强大功能。
8985

被折叠的 条评论
为什么被折叠?



