在c#中找到完全匹配

编程入门 行业动态 更新时间:2024-10-28 18:22:21
本文介绍了在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#中找到完全匹配

本文发布于:2023-11-03 07:32:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554578.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中找到

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!