首页 > 其他 > 详细

Regex Failure - Bug Fixing #2

时间:2015-12-18 10:22:43      阅读:174      评论:0      收藏:0      [点我收藏+]

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);
  }
}

 

Regex Failure - Bug Fixing #2

原文:http://www.cnblogs.com/chucklu/p/5056166.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!