2013-08-05 44 views
0

对于正则表达式,我是一个完整的新手,并且想知道是否有人可以帮助我。我不确定使用regEx是否是正确的方法,所以如果您有更好的想法,请随时留言。 (我会循环很多字符串)。regEx以不区分大小写的字符串

基本上,我想查找/替换一个字符串,用{}包装匹配并保留字符串的原始大小写。

例子:

Source: "The CAT sat on the mat."  
Find/Replace: "cat"  
Result: "The {CAT} sat on the mat." 

我想查找/替换工作只在第一次出现了,我还需要知道是否查找/替换确实匹配与否。

我希望我已经解释清楚了。

谢谢。

回答

5
Regex theRegex = 
    new Regex("(" + Regex.Escape(FindReplace) + ")", RegexOptions.IgnoreCase); 
theRegex.Replace(Source, "{$1}", 1); 

如果你想单词边界公差:

Regex theRegex = 
    (@"([\W_])(" + Regex.Escape(FindReplace) + @")([\W_])", RegexOptions.IgnoreCase) 
theRegex.Replace(str, "$1{$2}$3", 1) 
+0

我将如何使用它来只匹配第一次出现? – Trevor

+0

哦,看起来很简单...替换(...,1) – Trevor

+1

@Trevor这将工作,**但**没有一个选项,接受int和ignorecase。 –

1

如果你将通过许多字符串循环,那么也许正则表达式可能不是最好的主意 - 这是一个伟大的工具,但不是最快的。

下面是一个示例代码,也将工作:

 var str = "The Cat ate a mouse"; 
     var search = "cat"; 
     var index = str.IndexOf(search, StringComparison.CurrentCultureIgnoreCase); 
     if (index == -1) 
      throw new Exception("String not found"); //or do something else in this case here 
     var newStr = str.Substring(0, index) + "{" + str.Substring(index, search.Length) + "}" + str.Substring(index + search.Length); 

编辑:

正如评论指出的,上面的代码有一些问题。

因此,我决定尝试找到一种方法使其工作,而不使用正则表达式。不要误解我的意思,我喜欢Regex和下一个人一样。我主要是出于好奇才做到这一点。 ;)

这是我来到后:

public static class StringExtendsionsMethods 
{ 
    public static int IndexOfUsingBoundary(this String s, String word) 
    { 
     var firstLetter = word[0].ToString(); 
     StringBuilder sb = new StringBuilder(); 
     bool previousWasLetterOrDigit = false; 
     int i = 0; 
     while (i < s.Length - word.Length + 1) 
     { 
      bool wordFound = false; 
      char c = s[i]; 

      if (c.ToString().Equals(firstLetter, StringComparison.CurrentCultureIgnoreCase)) 
       if (!previousWasLetterOrDigit) 
        if (s.Substring(i, word.Length).Equals(word, StringComparison.CurrentCultureIgnoreCase)) 
        { 
         wordFound = true; 
         bool wholeWordFound = true; 
         if (s.Length > i + word.Length) 
         { 
          if (Char.IsLetterOrDigit(s[i + word.Length])) 
           wholeWordFound = false; 
         } 

         if (wholeWordFound) 
          return i; 

         sb.Append(word); 

         i += word.Length; 
        } 

      if (!wordFound) 
      { 
       previousWasLetterOrDigit = Char.IsLetterOrDigit(c); 
       sb.Append(c); 
       i++; 
      } 
     } 

     return -1; 
    } 
} 

但我不能居功这个!我在谷歌搜索here, on StackOverflow后发现这个,然后修改它。 ;)

使用此方法代替上述代码中的标准IndexOf

+2

你的速度明显更快(如果你做了一百万次,速度提高了8倍)**,但**它遭受了clbutt问题。如果你把Class当成str和ass来作为搜索,它会用Cl {ass}来代替Class。 –

+0

我只需要在匹配 – Trevor

+0

@ It'sNotALie首次发现/替换。嗯,公平点!我会看看我能不能做得更好。 – Shaamaan

1

试试这个:

class Program 
{ 
    const string FindReplace = "cat"; 
    static void Main(string[] args) 
    { 
     var input = "The CAT sat on the mat as a cat."; 
     var result = Regex 
      .Replace(
      input, 
      "(?<=.*)" + FindReplace + "(?=.*)", 
      m => 
      { 
       return "{" + m.Value.ToUpper() + "}"; 
      }, 
      RegexOptions.IgnoreCase); 
     Console.WriteLine(result); 
    } 
} 
相关问题