locked
Using "Replace" with multi-word phrases RRS feed

  • Question

  • User1445074224 posted

    Hi. I am using the following code:

    SqlCommand cmd2;
    string commandString2 = "SELECT [phrase], [url] FROM links WHERE active = @activeState";
    cmd2 = new SqlCommand(commandString2, conn);
    cmd2.Parameters.AddWithValue("activeState", 'Y');

    Simplified... the "links" table looks like this

    phrase                          url                                                active

    Tuesday                        www.tuesday.com                       Y

    Tuesday Morning         www.tuesdaymorning.com         Y 

    I am then using the following code to search through a page of text in a db table fro the phrases "Tuesday" and "Tuesday Morning" and replace them with links to two different urls.

    string tempText = (string)cmd3.ExecuteScalar(); // ... the page of text to search.
    
    // Get Search Term & Replace Term
    SqlDataReader rdr2 = cmd2.ExecuteReader();
    
    // Do the searching and replacing
    while (rdr2.Read())
            {
                tempText = tempText.Replace((string)rdr2["phrase"], "<a href=\"" + (string)rdr2["url"] + "\">" + (string)rdr2["phrase"] + "</a>");
            }
    rdr2.Close();

    OK. The problem is, this is working perfectly for "Tuesday" but not for "Tuesday Morning". In the latter case, The "Tuesday" of "Tuesday Morning" is being linked to the first URL and the "Morning" of "Tuesday Morning" is not affected. The code is stopping after "Tuesday" in "Tuesday Morning" and using the first row in the "links" table.

    If tempText was "Tuesday lasts longer than Tuesday Morning." I would get:

    <a href="www.tuesday.com">Tuesday</a>lasts longer than <a href="www.tuesday.com">Tuesday</a> Morning. 

    So, how can I use this method to search for multi-word phrases and recognise "Tuesday Morning" as different from "Tuesday"?

    Thanks in advance.

    Tuesday, May 30, 2017 1:01 PM

All replies

  • User-271186128 posted

    Hi banksidepoet,

    banksidepoet

    If tempText was "Tuesday lasts longer than Tuesday Morning." I would get:

    <a href="www.tuesday.com">Tuesday</a>lasts longer than <a href="www.tuesday.com">Tuesday</a> Morning. 

    So, how can I use this method to search for multi-word phrases and recognise "Tuesday Morning" as different from "Tuesday"?

    I suggest you could refer to the following code, you could use Regex to split the tempText, then using string.Replace method to replace the string.

    Code as below:

                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("phrase", typeof(string)), new DataColumn("url", typeof(string)), new DataColumn("actiove", typeof(string)) });
                dt.Rows.Add("Tuesday", "www.tuesday.com", "Y");
                dt.Rows.Add("Tuesday Morning", "www.tuesdaymorning.com", "Y");
    
    
                string str = "Tuesday lasts longer than Tuesday Morning";
    string spattern = @"(Tuesday\s?(?:Morning)?)"; string [] strlist = Regex.Split(str, spattern); List<string> outputitem = new List<string>();
    foreach(string item in strlist) { if (item.Length > 1) { var cc = dt.AsEnumerable().Where(c => c.Field<string>("phrase") == item.TrimEnd()).Select(c=>new {url = c.Field<string>("url") }).FirstOrDefault(); if (cc != null) {
    //you could add a tag on here outputitem.Add(item.Replace(item, cc.url)); } else { outputitem.Add(item); } } } Response.Write(string.Join(" ", outputitem));

    Then the output: www.tuesday.com lasts longer than  www.tuesdaymorning.com

    Best regards,
    Dillion

    Wednesday, May 31, 2017 9:10 AM
  • User303363814 posted

    I'm guessing you have 'Friday Afternoon' and 'Saturday lunchtime' and other such variations as well.  The ultimate would be to add some sort of 'priority' column to the 'links' table and process in order of priority.

    However, a simplified version may be sufficient.  Long phrases should be processed before short ones, so change your command to

    SELECT ...blah, blah... WHERE active = @activeState order by LEN(phrase) desc";

    (I think that SQL is correct - haven't used SQL for years. "LINQ rules OK!")

    Thursday, June 1, 2017 12:09 AM