到达那里需要一段时间,但这种模式:
(.*((?!X).B|B(?!Z).))|(^B)|(B$)
寻找(不是 X 的东西)B 或 B(不是 Z 的东西)。TDD代码如下:
[Test]
public void TestPattern()
{
const string pattern = "(.*((?!X).B|B(?!Z).))|(^B)|(B$)";
Assert.IsFalse(Regex.IsMatch("Hello", pattern));
Assert.IsTrue(Regex.IsMatch("Hello ABC", pattern));
Assert.IsTrue(Regex.IsMatch("Hello DBE", pattern));
Assert.IsTrue(Regex.IsMatch("Hello ABE", pattern));
Assert.IsTrue(Regex.IsMatch("Hello FBG", pattern));
Assert.IsTrue(Regex.IsMatch("Hello ABC World", pattern));
Assert.IsTrue(Regex.IsMatch("Hello DBE World", pattern));
Assert.IsTrue(Regex.IsMatch("Hello ABE World", pattern));
Assert.IsTrue(Regex.IsMatch("Hello FBG World", pattern));
Assert.IsTrue(Regex.IsMatch("ABC World", pattern));
Assert.IsTrue(Regex.IsMatch("DBE World", pattern));
Assert.IsTrue(Regex.IsMatch("ABE World", pattern));
Assert.IsTrue(Regex.IsMatch("FBG World", pattern));
Assert.IsTrue(Regex.IsMatch("Hello DBE World XBZ", pattern));
Assert.IsTrue(Regex.IsMatch("Hello ABE World XBZ", pattern));
Assert.IsTrue(Regex.IsMatch("Hello FBG World XBZ", pattern));
Assert.IsFalse(Regex.IsMatch("Hello XBZ", pattern));
Assert.IsTrue(Regex.IsMatch("Hello XB", pattern));
Assert.IsTrue(Regex.IsMatch("Hello BZ", pattern));
Assert.IsTrue(Regex.IsMatch("XB Hello", pattern));
Assert.IsTrue(Regex.IsMatch("BZ Hello", pattern));
Assert.IsTrue(Regex.IsMatch("B", pattern));
}