locked
match exact string RRS feed

  • Question

  • I´m trying to run this code that uses a string(ing) and a string array (splitwords).
    I need to run all the string(ing) and if it matches the string from the string array does something with that original string.

    Code:

    StringBuilder builder = newStringBuilder(); 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();

    With all the tests it runs ok, but if the word (in the splitwords) to match is "leite" and in the text it has the word "I need to run the string with the word leitelho in it" he thinks it  matches the word "leite".

    It returns "I need to run the string with the word LEITE lho in it"

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


    Monday, January 5, 2015 3:44 PM

Answers

  • One has to take the text and do each operation, which is a regex match, individually. Since this is ultimately a replace operation, use of the Regex.Replace method to work on our string would be the most effective for each word (snippet) to process.

    For each operation that the match finds such as the toUpper (or even other operations along with it), I would use the aggregate function which accumulates each changes and works through each individual words (snippets) to replace.

    By using the `\b` word boundary in the Regex pattern ensures that we are not matching the 'egg' in 'eggs' if that existed during our run (which is a situation which I did for the test in the words list below by doing 'egg' first in the list!) .

    Here is the example:

    var data = "I have a receipt with eggs but others with just one egg";
    var words = new List<string> { "have a receipt", "egg", "eggs" };
    
    var result = 
       words.Aggregate (data, 
       (currentLine, word) => Regex.Replace(currentLine, @"\b" + word + @"\b", word.ToUpper() ));
    							  
    							  
    /* Result is: 
    I HAVE A RECEIPT with EGGS but others with just one EGG */


    William Wegerson (www.OmegaCoder.Com)





    • Proposed as answer by Danny Rosales Monday, January 5, 2015 8:49 PM
    • Edited by OmegaMan Tuesday, January 6, 2015 2:55 PM
    • Marked as answer by Kristin Xie Tuesday, January 13, 2015 9:38 AM
    Monday, January 5, 2015 8:37 PM

All replies

  • Hi Leonor, 

    This following line is incomplete;

    ing = " " + ing + " ";

    Try the following code, it COMPILES and runs in Visual Studio just fine. BTW, if the split function is to work, you need to add a space between the words leite and lho. What does this phrase mean???

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; namespace MatchExactString { class Program { static void Main(string[] args) { //Console.WriteLine("enter words seperated by space"); //string[] ArrayOfWords = Console.ReadLine().Split(' '); String WordPhrase = "I need to run the string with the word leite << SPACE HERE >>lho in it"; string[] ArrayOfWords = WordPhrase.Split(' '); String x = ReturnString(ArrayOfWords);

    Console.Writeline(x); } static string ReturnString(string[] someWords) { StringBuilder builder = new StringBuilder(); string ing = "leite"; string[] splitwords = someWords; 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 ing = word.ToUpper(); } //builder.Append(ing); //builder.Append(@"}"); }//end foreach builder.Append(ing); builder.Append(@"}"); return builder.ToString(); } } }


    Noticed I have commented out the following lines;

    builder.Append(... ;

    Are you trying to return a JSON string ???



    UML, then code

    Monday, January 5, 2015 4:27 PM
  • Hi ! 

    Thanks for your reply.

    I not sure you understand what I needed to do I ´ll give a more detailed example:

    Text: "I have a receipt with eggs but others with just one egg"

    Words: "have a receipt", "eggs", "egg" 

    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"

    I cannot use the split because I may have situations like the above



    Monday, January 5, 2015 4:40 PM
  • Can't you sort the splitwords by length (descending) so that longer words get replaced first?
    Monday, January 5, 2015 6:12 PM
  • Probably something is incorrect inside your if. Show some details. To know where the word is found, use Match instead of IsMatch. It will offer you Index and Length, so that you can make the fragment uppercase.

    Monday, January 5, 2015 8:17 PM
  • One has to take the text and do each operation, which is a regex match, individually. Since this is ultimately a replace operation, use of the Regex.Replace method to work on our string would be the most effective for each word (snippet) to process.

    For each operation that the match finds such as the toUpper (or even other operations along with it), I would use the aggregate function which accumulates each changes and works through each individual words (snippets) to replace.

    By using the `\b` word boundary in the Regex pattern ensures that we are not matching the 'egg' in 'eggs' if that existed during our run (which is a situation which I did for the test in the words list below by doing 'egg' first in the list!) .

    Here is the example:

    var data = "I have a receipt with eggs but others with just one egg";
    var words = new List<string> { "have a receipt", "egg", "eggs" };
    
    var result = 
       words.Aggregate (data, 
       (currentLine, word) => Regex.Replace(currentLine, @"\b" + word + @"\b", word.ToUpper() ));
    							  
    							  
    /* Result is: 
    I HAVE A RECEIPT with EGGS but others with just one EGG */


    William Wegerson (www.OmegaCoder.Com)





    • Proposed as answer by Danny Rosales Monday, January 5, 2015 8:49 PM
    • Edited by OmegaMan Tuesday, January 6, 2015 2:55 PM
    • Marked as answer by Kristin Xie Tuesday, January 13, 2015 9:38 AM
    Monday, January 5, 2015 8:37 PM
  • Check out this link for further education...

    http://jasonneylon.wordpress.com/2010/03/15/refactoring-to-linq-part-2-aggregate-is-great/


    UML, then code

    Monday, January 5, 2015 10:01 PM
  • Please show us how you create "splitwords".

     

    Noam B.


    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    Tuesday, January 6, 2015 11:47 AM