正则表达式 - 如何匹配除特定模式之外的所有内容 [英] Regex - how to match everything except a particular pattern
问题描述
如何编写一个正则表达式来匹配任何不符合特定模式的字符串?我面临着必须匹配 (A 和 ~B) 模式的情况.
How do I write a regex to match any string that doesn't meet a particular pattern? I'm faced with a situation where I have to match an (A and ~B) pattern.
推荐答案
您可以使用前瞻断言:
(?!999)\d{3}
此示例匹配 999
以外的三个数字.
This example matches three digits other than 999
.
但如果您碰巧没有使用此功能的正则表达式实现(请参阅正则表达式风味的比较),您可能需要自己构建具有基本功能的正则表达式.
But if you happen not to have a regular expression implementation with this feature (see Comparison of Regular Expression Flavors), you probably have to build a regular expression with the basic features on your own.
仅具有基本语法的兼容正则表达式为:
A compatible regular expression with basic syntax only would be:
[0-8]\d\d|\d[0-8]\d|\d\d[0-8]
这也匹配任何不是 999
的三位数字序列.
This does also match any three digits sequence that is not 999
.
这篇关于正则表达式 - 如何匹配除特定模式之外的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!