Regex Failure - Bug Fixing #2

时间:2021-12-01 05:13:24

http://www.codewars.com/kata/55c423ecf847fbcba100002b/train/csharp

Oh no, Timmy's received some hate mail recently but he knows better. Help timmy fix his regex filter so he can be awesome again!

using System;
using NUnit.Framework;
System.Text.RegularExpressions [TestFixture]
public class Tests
{
[Test]
public static void FixedTest()
{
Assert.AreEqual("You're awesome! timmy!", Kata.filterWords("You're Bad! timmy!"));
Assert.AreEqual("You're awesome! timmy!", Kata.filterWords("You're MEAN! timmy!"));
Assert.AreEqual("You're awesome!! timmy!", Kata.filterWords("You're UGLY!! timmy!"));
Assert.AreEqual("You're awesome! timmy!", Kata.filterWords("You're horrible! timmy!"));
Assert.AreEqual("You're awesome!! timmy!", Kata.filterWords("You're HiDeOuS!! timmy!"));
Assert.AreEqual("You're awesomeish!! timmy!", Kata.filterWords("You're Meanish!! timmy!"));
}
}

主要是i的用法,

i

Use case-insensitive matching.

\b(?i)a(?-i)a\w+\b

"aardvark", "aaaAuto" in "aardvark AAAuto aaaAuto Adam breakfast"

using System;
using System.Text.RegularExpressions; public class Kata
{
public static string filterWords(string phrase)
{
string pattern = @"(?i)bad|mean|ugly|horrible|hideous\b";
string replacement = "awesome";
Regex rgx = new Regex(pattern);
return rgx.Replace(phrase, replacement);
}
}