Solved

Variables in regular expressions problem

Posted on 2004-10-25
233 Views
Last Modified: 2010-04-15
Hi

Can anyone tell me where I'm going wrong here.
string HighlightWord, Word1;

If I run the code below it works OK
HighlightWord = Regex.Replace(HighlightWord, "harry", "<strong>harry</strong>", RegexOptions.IgnoreCase);

But when I try and change harry to a variable it will not work, can anyone tell me what I need to change

HighlightWord = Regex.Replace(HighlightWord, "Word1", "<strong>Word1</strong>", RegexOptions.IgnoreCase);

Regards
Caz
0
Question by:BeginningWebDesign
    5 Comments
     
    LVL 8

    Expert Comment

    by:AaronReams
    is this what you're trying to do?

    string Word1 = "Word1";
    HighlightWord = Regex.Replace(HighlightWord, Word1, "<strong>Word1</strong>", RegexOptions.IgnoreCase);
    0
     
    LVL 6

    Expert Comment

    by:Chester_M_Ragel
    Its strange... Does HighlightWord string has "Word1"? If it has, surely it must get replaced by "<strong>Word1</strong>".
    0
     
    LVL 7

    Accepted Solution

    by:
    Hi,
    Hope u r trying this....

    string HighlightWord="Harry Test test harry";
    string Word1 = "Harry";
    HighlightWord = Regex.Replace(HighlightWord, Word1, "<strong>" + Word1 + "</strong>", RegexOptions.IgnoreCase);

    Check with this.....

    Sukumar
    0
     
    LVL 6

    Expert Comment

    by:etmendz
    BeginningWebDesign,

    There is something wrong with your question. Wanna fix your question, rephrase it or whatever,  so we'll understand better?

    :-)
    0
     

    Author Comment

    by:BeginningWebDesign
    Thanks sukumar_diya

    That fixed that problem, and has caused another problem. As I only asked how to highlight a variable which you answered, I have opened another question with the new problem.

    Caz
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Anonabox PRO Tor & VPN Router

    PRO is the most advanced way to fortify your privacy and online anonymity by layering the Tor network with VPN services. Use both together or separately, and without needing to download software onto your devices.

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video discusses moving either the default database or any database to a new volume.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now