我该怎么做呢?我已经看到了一个解决方案,不使用单个正则表达式 ruby因为 ruby不支持loookaround断言.但是在c#中有可能吗? [Test]public void RarArchiveFirstFileNameShouldMatch() { var regex = new Regex(
[Test] public void RarArchiveFirstFileNameShouldMatch() { var regex = new Regex(@"\.(rar|001)$", RegexOptions.IgnoreCase | RegexOptions.Singleline); Assert.That(regex.IsMatch("filename.001")); Assert.That(regex.IsMatch("filename.rar")); Assert.That(regex.IsMatch("filename.part1.rar")); Assert.That(regex.IsMatch("filename.part01.rar")); Assert.That(regex.IsMatch("filenamepart44.rar")); Assert.That(regex.IsMatch("filename.004"), Is.False); Assert.That(regex.IsMatch("filename.057"), Is.False); Assert.That(regex.IsMatch("filename.r67"), Is.False); Assert.That(regex.IsMatch("filename.s89"), Is.False); Assert.That(regex.IsMatch("filename.part2.rar"), Is.False); Assert.That(regex.IsMatch("filename.part04.rar"), Is.False); Assert.That(regex.IsMatch("filename.part11.rar"), Is.False); }这应该通过你的测试:
var regex = new Regex(@"(\.001|\.part0*1\.rar|^((?!part\d*\.rar$).)*\.rar)$", RegexOptions.IgnoreCase | RegexOptions.Singleline); Assert.That(regex.IsMatch("filename.001")); Assert.That(regex.IsMatch("filename.rar")); Assert.That(regex.IsMatch("filename.part1.rar")); Assert.That(regex.IsMatch("filename.part01.rar")); Assert.That(regex.IsMatch("filename.004"), Is.False); Assert.That(regex.IsMatch("filename.057"), Is.False); Assert.That(regex.IsMatch("filename.r67"), Is.False); Assert.That(regex.IsMatch("filename.s89"), Is.False); Assert.That(regex.IsMatch("filename.part2.rar"), Is.False); Assert.That(regex.IsMatch("filename.part04.rar"), Is.False); Assert.That(regex.IsMatch("filename.part11.rar"), Is.False);