#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2018
    Posts
    1
    Rep Power
    0

    Match except when proceeded and ending with /


    Hi everyone. I am learning c# on my own with the help of online forums, tutorials, books, etc... I recently stumbled upon regex and have started incorporating it in my project.

    I want to match a string variable but not if it is part of a longer word.

    I currently have this code:

    var s0 = MainInput.Text;
    for (int i = 0; i < STRING_FIND_LIST.Length; i++)
    {
    s0 = Regex.Replace(s0, @"\b" + STRING_FIND_LIST[i] + @"\b", "<abbr title=\"" + STRING_REPLACE_LIST[i] + "\">" + STRING_FIND_LIST[i] + "</abbr>");
    }
    MainInput.Text = s0;

    To match whole words only , I have the \b before and after my regex expression. This has worked perfectly until I into a situation when my string_find_list was part of a web address. for example, lets say I was searching for Canada and wanted to replace it with Canada Eh

    Canada -> Canada Eh
    Canada. -> Canada Eh.
    <p>Canada</p> -> <p>Canada Eh</p>

    The problem happens when I see this in text

    301 Moved Permanently -> 301 Moved Permanently Eh/En

    This obviously breaks the link....Is there anyway to stop regex from matching Canada when it is proceeded and/or ended with a /?

    Thanks!!
  2. #2
  3. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,896
    Rep Power
    9646
    Escape the search string so Regex doesn't try to interpret it.

IMN logo majestic logo threadwatch logo seochat tools logo