C# 正则表达式包装字符串,不区分大小写
对于正则表达式,我是一个完全的新手,我想知道是否有人能帮我。我不确定使用正则表达式是否是正确的方法,所以如果您有更好的想法,请随时加入。(我将通过许多字符串循环) 基本上,我希望查找/替换字符串,用{}包装匹配项并保留字符串的原始大小写 例如:C# 正则表达式包装字符串,不区分大小写,c#,regex,C#,Regex,对于正则表达式,我是一个完全的新手,我想知道是否有人能帮我。我不确定使用正则表达式是否是正确的方法,所以如果您有更好的想法,请随时加入。(我将通过许多字符串循环) 基本上,我希望查找/替换字符串,用{}包装匹配项并保留字符串的原始大小写 例如: Source: "The CAT sat on the mat." Find/Replace: "cat" Result: "The {CAT} sat on the mat." 我希望查找/替换只在第一次出现时工作,并且我还需要知道查
Source: "The CAT sat on the mat."
Find/Replace: "cat"
Result: "The {CAT} sat on the mat."
我希望查找/替换只在第一次出现时工作,并且我还需要知道查找/替换是否确实匹配
我希望我已经解释清楚了
多谢各位
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)
如果您将在许多字符串中循环,那么Regex可能不是最好的主意-它是一个很棒的工具,但不是最快的 下面是一个同样有效的示例代码:
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);
编辑:
如评论中所述,上述代码存在一些问题
所以我决定尝试找到一种不用正则表达式就可以工作的方法。别误会,我和下一个男人一样喜欢正则表达式。我这么做主要是出于好奇
以下是我遇到的情况:
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;
}
}
公共静态类StringExtensionsMethods
{
公共静态int IndexOfUsingBoundary(此字符串为s,字符串为单词)
{
var firstLetter=word[0]。ToString();
StringBuilder sb=新的StringBuilder();
bool-previouswasletterDigit=false;
int i=0;
而(ii+word.Length)
{
if(Char.isleterordigit(s[i+word.Length]))
wholeWordFound=false;
}
if(wholeWordFound)
返回i;
某人附加(字);
i+=字长;
}
如果(!wordFound)
{
previousWasLetterOrDigit=Char.IsleterOrdigit(c);
sb.附加(c);
i++;
}
}
返回-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);
}
}
类程序
{
常量字符串FindReplace=“cat”;
静态void Main(字符串[]参数)
{
var input=“猫像猫一样坐在垫子上。”;
var result=Regex
.替换(
输入,
“(?你的速度明显更快(如果你做一百万次,速度会提高8倍),但它有clbutt问题。如果你把类作为str,把ass作为search,它就会出现Cl{ass}”,而不是类。我只需要在match@It'sNotALie.Hmm,说得好!我会看看我是否能把它做得更好。我找到了一个解决方案-它绝对不短,但它应该按照OP的要求工作,并且应该比正则表达式快。:p有一些方法可以使.Net正则表达式引擎的性能极快.最重要的是预编译的正则表达式对象。第二个是表达式本身,它可以通过使用unicode值和缓存来加快速度。我忍不住觉得你们在用脏刷子给.net正则表达式着色。我如何使用它来只匹配第一次出现的对象?哦,看起来很简单…替换(…,1)@Trevor那会有用的,但是没有一个选项可以接受int和ignorecase。啊,没有注意到that@Trevor另外,要检查查找/替换是否成功,只需使用IsMatch(与regex实例和输入一起使用)。