在c#中找到完全匹配 [英] Find exact match in c#

查看:112
本文介绍了在c#中找到完全匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图开发这个包含字符串(ing)和字符串数组(splitwords)的代码。

我需要运行一个字符串,如果它匹配字符串数组中的字符串做某事使用原始字符串。



Im trying to develop this code that haves a string(ing) and a string array (splitwords).
I need to run a string and if it matches the string from the string array does something with that original string.

StringBuilder builder = new StringBuilder();

ing = " " + ing + " ";

builder.Append(@"{\rtf1\ansi");

foreach (string word in splitwords)
{

if (Regex.IsMatch(ing, @"(?<![\w])" + word + @"(?![\w])"))
{
// for example put the word in UPPERCASE
}

builder.Append(ing);
builder.Append(@"}");


return builder.ToString();



全部它运行的测试没问题,但是如果我有,那么


文字(ing):我有一个带鸡蛋的收据,但其他只有一个鸡蛋;



单词(分词):蛋,蛋,有收据



结果我得到使用上面的代码:我收到EGG的收据,但其他只有一个EGG



我需要的是:我收到了EGGS的收据,但其他人收到了只需一个EGG



如何更改我的if条件来解决这个问题?


With all the tests it runs ok, but if if I have

Text(ing): "I have a receipt with eggs but others with just one egg";

Words(splitwords): "egg","eggs", "have a receipt"

Result I get with the code above: "I HAVE A RECEIPT with EGG s but others with just one EGG"

What I need : "I HAVE A RECEIPT with EGGS but others with just one EGG"

How can I change my if condition to solve this problem?

推荐答案

示例代码:

sample code:
var splitwords =new string[] {"egg","eggs", "have a receipt"}; 
string input ="I have a receipt with eggs but others with just one egg";
foreach (string word in splitwords)
{
	var regex = new Regex(@"(?<![\w])" + word + @"(?![\w])", RegexOptions.IgnoreCase);
	input = regex.Replace(input, m=>m.ToString().ToUpper());
}
//inpt = "I HAVE A RECEIPT with EGGS but others with just one EGG"





只替换正则表达式的匹配项,那么它应该可以正常工作。



replace only the matching items of regex then it should work fine.


这篇关于在c#中找到完全匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆